SpringBoot和SpringJDBC多个数据源 [英] SpringBoot and SpringJDBC multiple datasources

查看:119
本文介绍了SpringBoot和SpringJDBC多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SpringBoot应用程序中使用两个数据源,但无法将第二个数据源自动连接。我已经尝试了很多东西,但这是我最近来的东西:

I am trying to use two datasources with my SpringBoot application and can't get the second datasource to autowire. I have tried many things but this is the closest I have come:

我的Yaml文件:

spring:
  first-datasource:
    url: MyURLString1
    username: User
    password: Password
    driver-class-name: oracle.jdbc.OracleDriver
  second-datasource:
    url: MyURLString2
    username: User
    password: Password
    driver-class-name: oracle.jdbc.OracleDriver

我的应用程序类:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.first-datasource")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

最后是我的DAO:

@Repository
public class MyDao {
    private static final String FIRST_SELECT = "select * from SomeTableInDB1";
    private static final String SECOND_SELECT = "select * from AnotherTableInDB2";

    @Autowired
    private JdbcTemplate firstJdbcTemplate;
    @Autowired
    @Qualifier("secondDataSource")
    private JdbcTemplate secondJdbcTemplate;

    List<DB1Entity> getDB1Entity(Long id) {
        return firstJdbcTemplate.query(FIRST_SELECT, new Object[] {id}, new BeanPropertyRowMapper(DB1Entity.class));
    }

    List<DB2Entity> getDB2Entity(Long id) {
        return secondJdbcTemplate.query(SECOND_SELECT, new Object[] {id}, new BeanPropertyRowMapper(DB2Entity.class));
    }
}

这是我到目前为止最接近的。我说这是最接近的,因为如果我删除@Qualifier,那么我的两个dao方法都可以正常工作,假设SECOND_SELECT语句对我的DB1是有效的SQL。一旦为非主要数据源输入@Qualifier,我就会得到一个自动装配错误,因为Spring需要一个Datasouce对象,而不是JdbcTemplate对象。这对我来说很奇怪,因为它可以与主数据源一起使用。

This is the closest I have come so far. I say it is closest because if I remove the @Qualifier then both of my dao methods actually work, assuming that the SECOND_SELECT statement is valid SQL for my DB1. Once I put in the @Qualifier for my non-primary datasouce then I get an autowire error because Spring is expecting a Datasouce object, not a JdbcTemplate object. That is weird to me as it does work with the primary datasource.

这是我的错误:

可以不是自动装配字段:私有org.springframework.jdbc.core.JdbcTemplate org.my.classpath.secondJdbcTemplate;嵌套的异常是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(value = secondDataSource)}

Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate org.my.classpath.secondJdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=secondDataSource)}

推荐答案

您创建类型为 DataSource 的bean,但是尝试使用 Autowire JdbcTemplate,这是不匹配的。您可能应该有这样的东西

You create the bean of type DataSource, but try to Autowire JdbcTemplate which is a mismatch. Your probably should have something like this

private JdbcTemplate jdbcTemplate1;
private JdbcTemplate jdbcTemplate2;

@Autowired
@Qualifier("firstDataSource")
public void setDataSource(DataSource dataSource){
    this.jdbcTemplate1=new JdbcTemplate(dataSource);
}

@Autowired
@Qualifier("secondDataSource")
public void setDataSource(DataSource dataSource){
    this.jdbcTemplate2=new JdbcTemplate(dataSource);
}

这篇关于SpringBoot和SpringJDBC多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆