I am using multiple datasource in my Spring Boot application.
In @EnableJpaRepositories, how do I add the base packages efficiently if I have a project structure like below:
Note - Repositories under all modules(module1, module2, module3) of com.project needs to access db1 except the repository under package(com.project.module3.submodule.repository) which needs to access db2.
com.project
|
com.project.module1.controller
|
com.project.module1.entity
|
com.project.module1.repository
|
com.project.module2.controller
|
com.project.module2.entity
|
com.project.module2.repository
|
com.project.module3.controller
|
com.project.module3.entity
|
com.project.module3.repository
|
com.project.module3.submodule.controller
|
com.project.module3.submodule.entity
|
com.project.module3.submodule.repository
What I tried so far:
//Method 1 works fine
//DB1 Config
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//, basePackages = {"com.project.module1.repository", "com.project.module2.repository", "com.project.module3.repository" })
//DB2 Config
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//, basePackages = {"com.project.module3.submodule.repository" })
---------------------
//Method 2 throws below error
//DB1 Config
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//, basePackages = {"com.project"})
//DB2 Config (works fine)
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//, basePackages = {"com.project.module3.submodule.repository" })
APPLICATION FAILED TO START
***************************
Description:
The bean 'SubmoduleRepo', defined in com.project.module3.submodule.repository.SubmoduleRepo defined in @EnableJpaRepositories declared on Db1Config, could not be registered. A bean with that name has already been defined in com.project.module3.submodule.repository.SubmoduleRepo defined in @EnableJpaRepositories declared on Db2Config and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Instead of specifying all the module repository packages in the DB1Config, is there a way to exclude submodule package. Because I have many module packages and its cumbersome to mention each repository package. How do I do this?
Thank you in advance!