繁体   English   中英

Spring 引导定义两个数据源

[英]Spring Boot defining two datasources

我有一个 Java 4 和 Spring Boot 2.4.0-SNAPSHOT 应用程序。

它需要访问两个单独的数据源。 我也在使用 Spring jdbc 来查询数据库。

我已经尝试了一个实现(见下文),但我得到了错误。

我有以下内容:

应用程序属性

# 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=

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();
    }
}

然后在 DAO 中,我定义了 jdbcTemplate。

公司联系DAOImpl.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;

现在当我启动 Spring Boot 时,我收到以下错误:

无法自动接线。 合格的 bean 必须是“JdbcTemplate”类型。

上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“approvalRequestDAOImpl”的bean时出错:通过字段“jdbcTemplate”表示的依赖关系不满足; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.jdbc.core.JdbcTemplate”类型的合格 bean:预计至少有 1 个符合自动装配候选资格的 bean。 依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("datasource2")}

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“approvalRequestDAOImpl”的bean时出错:通过字段“jdbcTemplate”表示的依赖关系不满足; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.jdbc.core.JdbcTemplate”类型的合格 bean:预计至少有 1 个符合自动装配候选资格的 bean。 依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("datasource2")} 原因:org.springframework.beans。 factory.NoSuchBeanDefinitionException:没有“org.springframework.jdbc.core.JdbcTemplate”类型的合格bean可用:预计至少有1个符合自动装配候选资格的bean。 依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("datasource2")}

堆栈跟踪与其他内容有关。 您的 JdbcTemplates需要数据源,它们本身不是数据源。 创建一个 JdbcTemplate 并注入数据源,而不是在 JdbcTemplates 上设置限定符,例如:

@Repository
public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {

    private JdbcTemplate jdbcTemplate;

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

更多信息在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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