Difference between @EntityScan and @ComponentScan
We can also use @EntityScan
to define some Entities form external jar.
The @ComponentScan
annotation is used to automatically create beans for every class annotated with @Component
, @Service
, @Controller
, @RestController
, @Repository
, ... and adds them to the Spring container (allowing them to be @Autowired
).
The @EntityScan
on the other hand does not create beans as far as I know. It only identifies which classes should be used by a specific persistence context. Since Spring boot 1.4 that includes JPA, MongoDB, neo4j, Cassandra and CouchBase.
Why they're not merged? Well, I'm not on the Spring team, but since they have different meanings, why should they be merged? The @EntityScan
should mainly be used to scan your entity packages, while the @ComponentScan
should scan all packages that contain Spring beans, so the following is very likely:
@ComponentScan("org.example.base")
@EntityScan("org.example.base.entities")
public class MyConfig {
}