The @ComponentScan annotation tells Spring where to look for classes annotated with @Component and its specializations (@Service, @Repository, @Controller, @RestController and @Configuration) so it can register them as beans automatically. Without it you would have to declare every bean by hand. In a Spring Boot application you rarely write it, because @SpringBootApplication already includes it.
By default it scans the package of the annotated class and all of its subpackages. You use an explicit @ComponentScan when your beans live outside that package tree, or when you want to filter what gets registered.
When the application context starts, Spring reads the class files under the configured packages, finds those carrying a stereotype annotation and registers a bean definition for each one. Dependency injection then wires them together.
With this configuration every annotated class under com.example.shop and its subpackages becomes a bean.
| Attribute | Purpose |
|---|---|
value / basePackages | Package names to scan |
basePackageClasses | Type-safe alternative: scan the package of each given class |
includeFilters | Extra types to register |
excludeFilters | Types to skip |
useDefaultFilters | Set to false to scan only what includeFilters names |
nameGenerator | Custom bean name strategy |
lazyInit | Make scanned beans lazy |
Prefer basePackageClasses to string names because it survives package renames.
Here OrderModule and BillingModule are empty marker classes placed in the packages to scan, and LegacyOrderService is left out. Filter types include ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, REGEX and CUSTOM.
@SpringBootApplication is a combination of @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan. So placing your main class in the root package, for example com.example.shop, means everything under it is found.
If your beans are in another tree, use the scanBasePackages attribute of @SpringBootApplication, or add @ComponentScan with the extra packages. Be careful: an explicit @ComponentScan on the main class replaces the default scan of its own package unless you list that package too.
Related annotations scan separately. @EntityScan finds JPA entities and @EnableJpaRepositories finds repositories, so moving code across package trees can break those even when component scanning works.
No qualifying bean of type ... usually means the class sits outside the scanned packages. Move it under the main class package or add the package to the scan.ConflictingBeanDefinitionException. Narrow the packages or add exclude filters.Normally no. @SpringBootApplication already scans its own package and subpackages. Add scanBasePackages or @ComponentScan only for packages outside that tree.
@ComponentScan discovers classes by package and annotation. @Import registers specific configuration classes you name explicitly.
Yes. The annotation is repeatable, or you can use @ComponentScans to group several.
Only if you add their packages. Libraries usually expose their beans through auto-configuration instead.
@ComponentScan is how Spring discovers beans from stereotype annotations. Spring Boot applies it by default through @SpringBootApplication, scanning from the main class package downwards. Use basePackageClasses, filters and scanBasePackages for custom layouts, and keep a clean package structure to avoid missing or duplicate beans.