I have a Java 4 and Spring Boot 2.4.0-SNAPSHOT application.
It needs to access two separate datasources. I am also using Spring jdbc to query the databases.
I have tried an implementation (see below), but I get errors.
I have the following:
application.properties
# pims datasource
spring.datasource1.driver-class-name=org.postgresql.Driver
spring.datasource1.url=jdbc:postgresql://localhost:5432/pims
spring.datasource1.username=postgres
spring.datasource1.password=
spring.jpa.database-platform=postgres
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
# approval datasource
spring.datasource2.driver-class-name=org.postgresql.Driver
spring.datasource2.url=jdbc:postgresql://localhost:5432/approval
spring.datasource2.username=postgres
spring.datasource2.password=
and
MultipleDBConfig.java
@Configuration
@ComponentScan(basePackages = "com.nexct")
public class MultipleDBConfig {
@Bean(name = "datasource1")
@ConfigurationProperties("spring.datasource1")
@Primary
public DataSource dataSource1(){
return DataSourceBuilder.create().build();
}
@Bean(name = "datasource2")
@ConfigurationProperties("spring.datasource2")
public DataSource dataSource2(){
return DataSourceBuilder.create().build();
}
}
Then in the DAO, I define the jdbcTemplate.
CompanyContactDAOImpl.java
@Repository
public class CompanyContactDAOImpl implements CompanyContactDAO {
@Autowired
@Qualifier("datasource1") // pims datasource
private JdbcTemplate jdbcTemplate;
ApprovalRequestDAOImpl.java
@Repository
public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {
@Autowired
@Qualifier("datasource2") // approval datasource
private JdbcTemplate jdbcTemplate;
Now when I start Spring Boot, I get the following error:
Could not autowire. Qualified bean must be of 'JdbcTemplate' type.
and
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalRequestDAOImpl': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("datasource2")}
and
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalRequestDAOImpl': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("datasource2")} at Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("datasource2")}