Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @EntityScan and @ComponentScan

I am trying to understand the difference here. I see that a class has been annotated with both of them with same package example :

@Configuration @EntityScan("some.known.persistence") @ComponentScan({ "some.known.persistence"}) public class ApiConfig {  } 

I understand the differences from API docs but want to understand in detail. Also does it mean that anything that is scanned by @ComponentScan is having wider visibility wrt Spring context and @EntityScan does not. If so using some attribute with @ComponentScan should have sufficed the need to bind in a JPA context, isn't it?

like image 431
Raghuveer Avatar asked Aug 11 '16 12:08

Raghuveer


People also ask

What does @ComponentScan annotation do?

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

What is the use of @SpringBootApplication?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.

When should I take EnableJpaRepositories?

The @EnableJpaRepositories annotation is used to create repository classes from Spring Data interfaces. All three annotation are often used together, but they are responsible for different things.

What is @EnableAutoConfiguration?

The @EnableAutoConfiguration annotation enables Spring Boot to auto-configure the application context. Therefore, it automatically creates and registers beans based on both the included jar files in the classpath and the beans defined by us.


2 Answers

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 {  } 
like image 59
g00glen00b Avatar answered Sep 30 '22 16:09

g00glen00b


We can also use @EntityScan to define some Entities form external jar.

like image 34
ahmednabil88 Avatar answered Sep 30 '22 16:09

ahmednabil88