When defining an EntityManager in a Spring Java Config class, I can add the base packages to scan for Entity classes by calling a method on the corresponding builder:
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
// Some other configuration here
builder.packages("org.foo.bar", "org.foo.baz");
return builder.build();
}
I need something similar for the places where Spring looks for Repository Interfaces. The usual way is using the @EnableJpaRepositories
annotation:
@EnableJpaRepositories(basePackages = {"org.foo.barbaz"})
But I would like to have a dynamical way for defining these packages similar to the way above for the Entity locations. Something like this:
public SomeJpaRepositoryFactoryBean entityManagerFactory(JpaRepositoryFactoryBuilder builder) {
// Some other configuration here
builder.packages("org.foo.barbaz");
return builder.build();
}
Is there a way to do this with the current Spring Data JPA release is it simply not meant to be done this way?
What your looking for is
@EntityScan
but it's only available in Spring Boot. The configuration you can annotate in Spring Data JPA is documented here https://docs.spring.io/spring-data/jpa/docs/2.0.8.RELEASE/reference/html/#jpa.java-configWithout Spring Boot (plain Spring MVC setup)
@EnableJpaRepositories
can be used on more than one@Configuration
class. That said, every module can declare its own repositories by having an own configuration class:Spring Data JPA then counts in all of them (
package1
andpackage2
).Although this is still not a programmatical way, it solves my problem.
This problem also puzzled me for almost a week.I debug the "spring application context refresh" code and
org.springframework.data.jpa.repository.config.JpaRepositoriesRegistrar
line by line and then I solve the problem.I customize my
EnableJpaRepository
annotation andJpaRepositoriesRegistrar
,then I can do anything inAbdJpaRepositoriesRegistrar
("abd" is my customized classes's prefix).This problem also puzzled me for almost a week.I debug the "spring application context refresh" code and
org.springframework.data.jpa.repository.config.JpaRepositoriesRegistrar
line by line and then I solve the problem.AbdEnableJpaRepositories.java
AbdJpaRepositoriesRegistrar.java
AbdRepositoryBeanDefinitionRegistrarSupport.java
AbdAnnotationRepositoryConfigurationSource.java. You can override
getBasePackages
then you can return any packages you want.How to use?Here is configuration file. PrimaryJpaConfiguration.java
You can add
spring.jpa.base-packages
config to your application.properties. For example:spring.jpa.base-packages=com.foo.a,com.bar.b
,and the repositories and entities under those packages "com.foo.a" and "com.bar.b" will be added to the spring Ioc container.You can use
@AutoConfigurationPackage
annotation to add your child module's package to scan-packages.@EnableJpaRepositories
from your child moduleAdd
@AutoConfigurationPackage
class to the top directory of your child module (similar to@SpringBootApplication
, you must put this class to the top-most directory to scan all subpackages):Create
spring.factories
file under/resources/META-INF/spring.factories
of your child module and add the configuration class:org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.child.package.ChildConfiguration
Now you can
@Autowired
your repository from your core project. (Tested and worked)