Spring boot - how to configure multiple datasources

左心房为你撑大大i 提交于 2019-11-29 12:02:17

Here is complete solution to your problem ...

Your configuration classes will look like this :

MySqlConfiguration.java

@Configuration
public class MySqlConfiguration {

   @Bean(name = "dMySql")
   @ConfigurationProperties(prefix = "spring.datasource.d.int-mysql")
   public DataSource mysqlDrupalDataSource() {
     return DataSourceBuilder.create().build();
   }

   @Bean(name = "dJdbc")
   public JdbcTemplate drupalJdbcTemplate(@Qualifier("dMySql") DataSource dMySql) {
      return new JdbcTemplate(dMySql);
   }

   @Bean(name = "mMySql")
   @ConfigurationProperties(prefix = "spring.datasource.m.int-mysql")
   public DataSource mysqlDrupalDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "mJdbc")
   public JdbcTemplate drupalJdbcTemplate(@Qualifier("mMySql") DataSource mMySql) {
      return new JdbcTemplate(mMySql);
   }
}

OracleConfiguration.java

@Configuration
public class OracleConfiguration {

   @Primary
   @Bean(name = "tOracle")
   @ConfigurationProperties(prefix = "spring.datasource.test-oracle")
   public DataSource heOracleDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "tOracleJdbc")
   public JdbcTemplate jdbcTemplate(@Qualifier("tOracle") DataSource tOracle) {
      return new JdbcTemplate(tOracle);
   }

   @Bean(name = "iOracle")
   @ConfigurationProperties(prefix = "spring.datasource.int-oracle")
   public DataSource heOracleDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "iOracleJdbc")
   public JdbcTemplate jdbcTemplate(@Qualifier("iOracle") DataSource iOracle) {
      return new JdbcTemplate(iOracle);
   }
}

and in your DAO class , you can autowire the JdbcTemplate like this :

@Repository
public class DAOImpl implements DAOInterface {

    @Autowired
    @Qualifier("dJdbc")
    private JdbcTemplate dJdbc;

    @Autowired
    @Qualifier("mJdbc")
    private JdbcTemplate mJdbc;

    @Autowired
    @Qualifier("tOracleJdbc")
    private JdbcTemplate tOracleJdbc;

    @Autowired
    @Qualifier("iOracleJdbc")
    private JdbcTemplate iOracleJdbc;

    @Override
    public Map<String, Object> getBasicStudentInfo(String MAIL) {
        return dJdbc.queryForMap(GET_BASIC_STUDENT_INFO, new Object[]{MAIL});
    }

    .
    .
    .
}

Note: Make Sure to annotate one of DataSource with @Primary annotation

My setup: spring-boot version 1.2.5.RELEASE

I succeeded in running a setup like this, with the jdbc being created with the correct DataSources by adding a @Qualifier in each JDBC method creation

So, for every JDBC method you should match the qualifying datasource like this

@Bean(name = "dJdbc")
public JdbcTemplate drupalJdbcTemplate(@Qualifier("dMySql") DataSource dMySql) {
    return new JdbcTemplate(dMySql);
}

No matter you choose for @Primary, using the @Qualifier for every JDBC should work good. Autowiring jdbcTemplates in repositories, and using @Qualifier for them is ok also.

In your DAO you could wire in additional jdbctemplates. Then at runtime you can pick which one to use.

@Repository
public class DAOImpl implements DAOInterface {

@Autowired
@Qualifier("tOracle")
private JdbcTemplate testJdbc;

@Autowired
@Qualifier("intOracle")
private JdbcTemplate intJdbc;

@Override
public Map<String, Object> getBasicStudentInfo(String MAIL, String source) {
    if ("TEST".equals(source)){
          return testJdbc.queryForMap(GET_BASIC_STUDENT_INFO, new Object[]{MAIL});
    }else {
          return intJdbc.queryForMap(GET_BASIC_STUDENT_INFO, new Object[]{MAIL});       
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!