2016-05-17 44 views
1

我想与我的SpringBoot应用程序一起使用两个数据源,无法获得第二个数据源自动装配。我已经尝试了很多事情,但,这是我来最接近:SpringBoot和SpringJDBC多个数据源

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

终于吾道:

@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对象。这对我来说很奇怪,因为它与主要数据源一起工作。

这是我的错误:

无法自动装配领域:私人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)}

+1

您创建DataSource类型的bean,但创建了Autowire JdbcTemplate。你可能应该有这样的'私人JdbcTemplate jdbcTemplate1; 私人JdbcTemplate jdbcTemplate2; (DataSource) @Autowired @Qualifier(“firstDataSource”) public void setDataSource(DataSource dataSource){ this.jdbcTemplate1 = new JdbcTemplate(dataSource); } @Autowired @Qualifier( “secondDataSource”) 公共无效的setDataSource(DataSource的数据源){ this.jdbcTemplate2 =新的JdbcTemplate(数据源); } ' – lenach87

+0

我曾尝试过几乎与此相同的内容,但收到有关空URL的错误。我一定错了什么,因为我复制了你的代码并且它工作。谢谢。我相信这是愚蠢的。 – Gremash

+0

@ lenach87这是正确的解决方案,我认为这个问题很好。发表一个答案,我会将其标记为正确的。 – Gremash

回答

1

您创建类型的bean DataSource,但尝试Autowire这是一个不匹配的JdbcTemplate。你或许应该有这样的事情

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

理想的情况下,而不是授权,数据源的一个应该被标记为大多数通过注释的默认接线的初步工作。另外,我们需要为每个数据源分别创建TransactionManagers,否则Spring将不知道如何执行事务。以下是应该如何完成的完整示例

@Primary 
@Bean(name = "dataSource") 
@ConfigurationProperties(prefix="datasource.mysql") 
public DataSource dataSource() { 
    return DataSourceBuilder.create().build(); 
} 

@Primary 
@Bean(name = "transactionManager") 
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { 
    return new DataSourceTransactionManager(); 
} 

@Bean(name = "postGresDataSource") 
@ConfigurationProperties(prefix="datasource.postgres") 
public DataSource postgresDataSource() { 
    return DataSourceBuilder.create().build(); 
} 

@Bean(name = "postGresTransactionManager") 
public DataSourceTransactionManager transactionManager(@Qualifier("postGresDataSource") DataSource dataSource) { 
    return new DataSourceTransactionManager(); 
} 

@Transactional(transactionManager="postGresTransactionManager") 
public void createCustomer(Customer cust) { 
    customerDAO.create(cust); 
} 

// specifying a transactionManager attribute is optional if we 
// want to use the default transactionManager since we 
// already marked one of the TM above with @Primary 
@Transactional 
public void createOrder(Order order) { 
    orderDAO.create(order); 
} 

希望这有助于。

0

这里还提供了另一种“不工作的”局面弄得我好几天:

构式在Springboot应用两个同类型的数据源时

,该@Qualifier并不如预期回暖权豆类工作。它的行为就像Spring框架无法识别的一样。

原因是当使用@SpringbootApplication注释包含@EnableAutoConfiguration注释,它在Springboot中将自动配置它为用户提供的数据源。

这会对@Qualifier的行为造成严重影响。