简体   繁体   中英

Spring Boot defining two datasources

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")}

The stack trace is related to something else. Your JdbcTemplates require a datasource, they're not datasources by themselves. Create a JdbcTemplate and inject the datasource rather than setting the qualifiers on the JdbcTemplates, for instance:

@Repository
public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {

    private JdbcTemplate jdbcTemplate;

    public ApprovalRequestDAOImple(@Qualifier("datasource2") DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }
}

More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM