I need to make a query to number of databases. The number of databases can vary.
It looks convinient to have an ability to iterate them through a list.
Is it a good idea, or is it at least possible to have a list of repositories connected to different datasources or there is another better way?
I've already read How to use 2 or more databases with spring? . For implementing it I have to know the number of databases, but it can vary.
I tried:
@Entity
public class Member{}
public interface MemberRepository extends CrudRepository<Member, Long> {
}
@Component
@ConfigurationProperties("myClass")
public class MyClass {
@Autowired
private MemberRepository memberRepository;
}
@Component
@ConfigurationProperties("myClassHub")
public class MyClassHub {
@Autowired
private List<MyClass> myClasses;
}
This looks logical for me. But I cannot understand how to configure this list items. Such configuration does not work.
application.yml
myClassHub:
myClasses:
-
spring:
datasource:
url: jdbc:mysql://url
username: username
password: password
-
spring:
datasource:
url: jdbc:mysql://url
username: username
password: password
in pom.xml I have:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
I also thought about using AbstractRoutingDataSource, but the architecture of classes will become too complicated and not logical.