How to register dynamically created class file for @EnableJpaRepository

9 views
Skip to first unread message

Amit Singh

unread,
Apr 6, 2023, 9:14:22 AM4/6/23
to Getting started with Spring Framework
Hello All,

I am trying to create wrapper for integrating multiple databases in spring boot.
In order to scan Repositories, i am creating Configuration class file annotated with @EnableJpaRepositories at runtime.

Steps : 
1. Read multiple database configurations for some config file.
2. Create @Configuration class file  for each database at runtime
3. Register this configuration bean in spring boot at runtime.

Below is my code : 

My dynamically created @Configuration class file looks something this : 
Screenshot 2023-04-06 at 6.32.24 PM.png

I am modifying the attributes of @EnableJpaRepositories at runtime and then registering the bean. Below is the code for that 
Screenshot 2023-04-06 at 6.41.44 PM.png

Problem is : when i am running the code bean is getting registered, but code erros out when i @Autowire any repository class. It says "required a bean of type XYZ Repository not found".

Ideally when the bean is registered @EnableJpaRepositories with updated basepackageName it should have scanned them right ?


Please help me here, what am i doing wrong here

Thanks
Amit
Reply all
Reply to author
Forward
0 new messages