定义两个数据源的 Spring 引导



我有一个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 启动时,出现以下错误:

无法自动连线。合格的 bean 必须是"JdbcTemplate"类型。

上下文初始化期间遇到的异常 - 取消 刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为"approvalRequestDAOImpl"的 Bean 时出错:不满意 通过字段"jdbc模板"表示的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No 类型为"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 时出错:不满意 通过字段"jdbc模板"表示的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No 类型为"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: No qualified bean 类型为"org.springframework.jdbc.core.JdbcTemplate"可用: 预计至少有 1 个 Bean 符合自动连线候选条件。 依赖项注释: {@org.springframework.beans.factory.annotation.Autowired(required=true(, @org.springframework.beans.factory.annotation.Qualifier("datasource2"(}

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

@Repository
public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {
private JdbcTemplate jdbcTemplate;
public ApprovalRequestDAOImple(@Qualifier("datasource2") DataSource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
}

更多信息在这里

最新更新