@ComponentScan — How Spring Discovers Beans
Fast recall
Quick revision
Core answer
@Component, @Service, @Repository, @Controller, @Configuration) to register as beans. Without explicit basePackages, it scans the package of the annotated class and all subpackages — which is why placing your @SpringBootApplication-annotated main class at the root of your Java package structure is so important. You can narrow the scan with `basePackages` or `basePackageClasses` (type-safe alternative — refactoring-friendly), and filter beans with `includeFilters` / `excludeFilters` (ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, REGEX, CUSTOM). Spring Boot's @SpringBootApplication combines @Configuration, @EnableAutoConfiguration, and @ComponentScan — you rarely write @ComponentScan explicitly in a Boot app.Whether you understand the default scan root (class's package and below), the role in @SpringBootApplication, and how to narrow/filter the scan
Placing the main class in a deep package and having beans in sibling packages go undiscovered; using basePackages with string literals rather than basePackageClasses with type references
Explain the package-structure implication of @SpringBootApplication's implicit scan; show basePackageClasses as a refactor-safe alternative to basePackages strings
Interview-ready guide
Interview answer
Spring can only inject beans it knows about — and it only knows about beans in packages it scanned. That single fact explains most of the I annotated my class but Spring says it does not exist debugging sessions.
The default scan boundary
@SpringBootApplicationincludes@ComponentScanwith no explicitbasePackages— it scans the package of the main class and all its sub-packages- A service in
com.example.common.utilsis invisible if the main class is incom.example.app— the scanner never reached that package - Convention: put your main class in the root package of your project so all modules fall under the scan boundary
How scanning works internally
- Spring uses ASM bytecode reading — not class loading — to inspect annotations; this keeps startup fast even with broad boundaries
@ComponentScansupportsincludeFiltersandexcludeFiltersto control which classes register;FilterType.ANNOTATIONcan exclude test-only beans from production contexts
Performance at scale
- In large monoliths, scanning thousands of classes adds seconds to startup
- Add
spring-context-indexeras an annotation processor to generate a compile-time component index that Spring reads instead of scanning at runtime
Type-safe scanning
- Prefer
basePackageClassesoverbasePackages— class references survive package renames; string literals don't
When something is not found, check the scan boundary first. When startup is slow, check how wide the scanned package tree is. Those two rules solve most component scan problems.
@ComponentScan — How Spring Discovers Beans
Direct Answer
@ComponentScan tells Spring where to look for stereotype-annotated classes (@Component, @Service, @Repository, @Controller, @Configuration) to register as beans. Without explicit basePackages, it scans the package of the annotated class and all subpackages — which is why placing your @SpringBootApplication-annotated main class at the root of your Java package structure is so important. You can narrow the scan with basePackages or basePackageClasses (type-safe alternative — refactoring-friendly), and filter beans with includeFilters / excludeFilters (ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, REGEX, CUSTOM). Spring Boot's @SpringBootApplication combines @Configuration, @EnableAutoConfiguration, and @ComponentScan — you rarely write @ComponentScan explicitly in a Boot app.
Common Mistakes & Pitfalls
Placing the main class in a deep package and having beans in sibling packages go undiscovered; using basePackages with string literals rather than basePackageClasses with type references
Check your understanding
Answer these without looking back at the article.
- Why should your @SpringBootApplication main class live at the root of your package structure?
- What's the difference between basePackages and basePackageClasses?
- How do includeFilters and excludeFilters affect component scanning?
- How does @ComponentScan interact with auto-configuration classes from starter JARs?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.