1. Introduction
When creating Spring applications we might need to specify a list of packages that contain our entity classes. Similarly, we might need to specify list of Spring beans to be initialized. We use the annotations @EntityScan
and @ComponentScan
annotations.
In this short tutorial, we’ll discuss the cases where @EntityScan
and @ComponentScan
annotations are used in Spring applications and their differences.
2. @EntityScan
@EntityScan
configures the base packages used by auto-configuration when scanning for entity classes.
Using @EntityScan will cause auto-configuration to:
- Set the packages scanned for JPA entities.
- Set the initial entity set used with Spring Data MongoDB, Neo4j, Cassandra and Couchbase mapping contexts.
One of basePackageClasses()
, basePackages()
or its alias value()
may be specified to define specific packages to scan. In the absence of explicit package definitions, scanning will default to the package of the annotated class.
While writing a Spring application, we create Entity class by annotating it with @Entity
annotation. There are two approaches to place the entity classes in the project:
- In the application main package or its sub-packages.
- In a package which is altogether different than the root package.
In the first approach, the @EnableAutoConfiguration
annotation enables Spring to auto-configure the application.
In the second approach, we provide the information about the packages where entities can be found within the application using @EntityScan
. Using @EntityScan
disables the Spring Boot auto-configuration scanning for entities.
@Configuration @EntityScan("com.learnitweb.testpackage") public class EntityScanDemo { // ... }
2. @ComponentScan
Similar to @EntityScan
, you may want Spring to use only a certain specific beans. You can use @ComponentScan
for this case.@ComponentScan
configures component scanning directives for use with @Configuration
classes. You need to provide either basePackageClasses()
or basePackages()
(or its alias value()
) to define specific packages to scan. If no specific packages are defined, the scanning process will start recursively starting from the package of the class that declares this annotation. basePackages()
specifies the base packages to scan for annotated components. basePackageClasses specified the classes. The package of each class specified will be scanned.
@Configuration @ComponentScan( basePackages = {"com.learnitweb.testpackage"}, basePackageClasses = DemoBean.class) public class ComponentScanExample { // ... }
3. @EntityScan vs. @ComponentScan
As we saw while discussing @EntityScan
and @ComponentScan
, the purpose and intention of these annotations is different. @EntityScan
is used when you want to specify the entities to be scanned by the Spring whereas @ComponentScan
is used when you want to specify the beans to be scanned by the Spring.
4. Conclusion
In summary, grasping the differences between @EntityScan
and @ComponentScan
is crucial for configuring Spring applications effectively. Although both annotations facilitate component scanning, they serve distinct roles within the Spring ecosystem.
@ComponentScan
primarily identifies Spring-managed components such as beans, services, and controllers. It enhances application modularity and facilitates dependency injection.
On the other hand, @EntityScan
is specifically tailored for JPA (Java Persistence API) applications. It allows Spring Data to detect and manage entity classes, enabling seamless integration with databases.