Sometimes you may have noticed that some of the spring application projects (specially spring boot applications) uses @EntityScan and @EnableJpaRepositories annotations as a part of configuring Spring Data JPA support for the application.
But some of the spring boot applications managed to complete their configurations and run the applications with Spring Data JPA WITHOUT those two annotations.
You might be having a little confusion about the real usages of those two annotations and when to use them? that is fine. The purpose of this article is to describe about the real usages of those two annotations and giving a full picture/idea of how to and when to use them properly.
What is the Spring Boot main application package?
It is the package that contains the Spring Boot main configuration class that is annotated with @SpringBootApplication annotation.
@SpringBootApplication annotation
This annotation automatically provides the features of the following annotations
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
Spring Boot Auto-Configuration Feature with @EnableAutoConfiguration
If you want to get the maximum advantage of spring boot’s auto configuration feature, it is expected to put all your class packages under spring boot main application package (directly in main package or indirectly as sub packages).
The @EnableAutoConfiguration will scan the main package and its sub packages when executing the spring boot auto configuration feature for class path dependencies. If any class or package that is outside from the main application package and it is required for completing auto configuration for some dependency, then should be declared in the main configuration class properly (with related annotation).
Then the @EnableAutoConfiguration will scan for those declared packages for detecting the required classes in the process of completing/doing the auto configuration for the application dependency declared in the class path. Those can de described as follows.
@EnableJpaRepositories
This will enable the JPA repositories that contains in the given package(s).
For instance, Enabling auto configuration support for Spring Data JPA required to know the path of the JPA the repositories. By default, it will scan only the main application package and its sub packages for detecting the JPA repositories. Therefore, if the JPA repositories are placed under the main application package or its sub package, then it will be detected by the @EnableAutoConfiguration as a part of auto configuring the spring based configurations. If the repository classes are not placed under the main application package or its sub package, then the relevant repository package(s) should be declared in the main application configuration class with @EnableJpaRepositories annotation. Then this will enable the JPA repositories contains in the given/declared package(s).
e.g:-
@EnableJpaRepositories(basePackages = "com.springbootdev.examples.jpa.repositories")
@EntityScan
If the entity classes are not placed in the main application package or its sub package(s), then it is required to declare the package(s) in the main configuration class with @EntityScan annotation. This will tells spring boot to where to scan for detecting the entities for the application. Basically @EnableAutoConfiguration will scan the given package(s) for detecting the entities.
e.g:-
@EntityScan(basePackages = "com.springbootdev.examples.entity")
Lets look at some fun and real code examples. Here i am not going to explain you the Spring Data JPA here. It has already been discussed in my following article.
Click here to go to Spring Data JPA example
You can download the source code of this article from GitHub.
If you open the project in your IDE, you will notice that repository and entity packages are not placed in the main application package.
main application package
com.springbootdev.examples
JPA repository classes are in package
com.springbootdev.domain.repository
entity classes are in package
com.springbootdev.domain.entity
Therefore the entity classes location and JPA repositories location should be declared and enabled with @EntityScan and @EnableJpaRepositories annotations respectively. Otherwise the application will fail to load.
Please refer the following Spring Boot main configuration class.
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.autoconfigure.domain.EntityScan; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
@SpringBootApplication | |
@EntityScan(basePackages = {"com.springbootdev.domain.entity"}) | |
@EnableJpaRepositories(basePackages = {"com.springbootdev.domain.repository"}) | |
public class SpringBootDataJpaExampleApplication | |
{ | |
public static void main(String[] args) | |
{ | |
SpringApplication.run(SpringBootDataJpaExampleApplication.class, args); | |
} | |
} |
what if we scan the entire ‘com.springbootdev.domain’ package. Do we need to scan the entites and repositories again?
LikeLike
Yes. you may need. This is because ‘com.springbootdev.domain’ is not the main package.
LikeLike