Difference between EntityScan and ComponentScan
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
@ComponentScan and @EntityScan are two distinct Spring Boot annotations that scan the classpath for different types of classes. @ComponentScan discovers Spring-managed beans (classes annotated with @Component, @Service, @Repository, @Controller) and registers them in the application context. @EntityScan discovers JPA entity classes (annotated with @Entity) and registers them with the JPA EntityManagerFactory. They serve different purposes and operate on different class types — confusing them is a common source of "bean not found" and "entity not managed" errors.
@ComponentScan
@ComponentScan tells Spring where to look for beans. By default, @SpringBootApplication includes @ComponentScan scanning the package of the main class and all sub-packages:
To scan additional packages outside the default:
@ComponentScan detects classes with these stereotype annotations:
| Annotation | Purpose |
@Component | Generic Spring-managed bean |
@Service | Business logic layer |
@Repository | Data access layer (adds exception translation) |
@Controller | Spring MVC web controller |
@RestController | REST API controller (@Controller + @ResponseBody) |
@Configuration | Configuration class with @Bean methods |
@EntityScan
@EntityScan tells JPA (Hibernate) where to find @Entity classes. It does not register them as Spring beans — it registers them with the EntityManagerFactory so Hibernate can map them to database tables:
If your entities are in a package outside the main application package, you need @EntityScan:
Without @EntityScan, Hibernate only discovers entities in the main application package and its sub-packages.
When You Need Each Annotation
Note that @EnableJpaRepositories is a third annotation that scans for Spring Data JPA repository interfaces — it is separate from both @ComponentScan and @EntityScan.
Side-by-Side Comparison
| Feature | @ComponentScan | @EntityScan |
| What it scans for | @Component, @Service, @Repository, @Controller | @Entity, @Embeddable, @MappedSuperclass |
| Registers with | Spring ApplicationContext | JPA EntityManagerFactory |
Included in @SpringBootApplication | Yes (implicitly) | Yes (for the main package only) |
| When to add explicitly | Beans in external packages | Entities in external packages |
| Module | spring-context | spring-boot-autoconfigure |
Configuring with Spring Boot Properties
You can also configure entity scanning via application.properties instead of annotations:
However, annotation-based scanning with @EntityScan is the standard approach in Spring Boot.
Common Pitfalls
- Using
@ComponentScanto find JPA entities:@ComponentScandiscovers Spring beans, not JPA entities. Adding@ComponentScan("com.example.domain")does not make Hibernate aware of@Entityclasses in that package. You need@EntityScanfor entities. - Using
@EntityScanto find Spring beans: Conversely,@EntityScandoes not register classes as Spring beans. If your@Serviceclass is in a package only covered by@EntityScan, it will not be available for dependency injection. Use@ComponentScanfor beans. - Forgetting
@EnableJpaRepositoriesalongside@EntityScan: When entities are in an external package, their Spring Data repositories are usually in the same package.@EntityScanfinds the entities, but you also need@EnableJpaRepositoriesto discover the repository interfaces. - Overriding
@SpringBootApplicationdefault scanning: Adding@ComponentScanwith explicitbasePackagesreplaces the default scan of the main class package. If you specify@ComponentScan("com.example.shared")without also including the main package, beans in the main package are no longer discovered. - Duplicate bean registration from overlapping scans: If two
@ComponentScanbase packages overlap (e.g.,com.exampleandcom.example.service), beans in the overlapping area are scanned twice. Spring usually handles this gracefully by ignoring duplicates, but customBeanDefinitionRegistryPostProcessorconfigurations may cause conflicts.
Summary
@ComponentScandiscovers Spring beans (@Component,@Service,@Repository,@Controller) and registers them in the Spring context@EntityScandiscovers JPA entities (@Entity,@Embeddable) and registers them with the HibernateEntityManagerFactory- Both are implicitly included in
@SpringBootApplicationfor the main class package and its sub-packages - Add explicit annotations only when classes live outside the main application package (common in multi-module projects)
- Also add
@EnableJpaRepositorieswhen JPA repository interfaces are in external packages - Never use one annotation when you need the other — they serve completely different purposes

