繁体   English   中英

在 Spring Boot (v2.6.6) 中,如何从两个数据源中提取并分别访问它们?

[英]In Spring Boot (v2.6.6), how can I pull from two data sources and have them separately accessible?

我有两个同时需要实例的数据库,它们在 oracle weblogic 服务器上运行,而不是在本地运行。 在配置器 class 中,我指定了源并测试了连接,两个数据库都正确提取数据。 (一次使用一个,或使用一个作为@Primary)

@Configuration
public class appDatabaseConfiguration {

 @Bean
 @Primary
 public DataSource dataSourceA() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate corpdwJndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBAProperties>");
 }
 
 @Bean
 public DataSource dataSourceB() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate jndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBBProperties>");
 }
}

但是,我无法使用 @Qualifier 来告诉实例化配置中要选择的 bean-

@Service
public class DaoImpl implements Dao{
    @Autowired
    @Qualifier("dataSourceA")
    private JdbcTemplate dataSourceA;
    
    @Autowired
    @Qualifier("dataSourceB")
    private JdbcTemplate dataSourceB;
    
    public String getData() {

         resultsA = dataSourceA.queryForObject("SELECT COUNT(*) FROM TABLE", String.class);
                
         resultsB = dataSourceB.queryForObject("SELECT COUNT(*) FROM TABLE", String.class);     
        
         return resultsA + resultsB;
    }
}

不使用@Qualifier 时:DataSourceA 查询将成功,但 DataSourceB 将失败(表或视图不存在错误)- 因为它正在为两个实例拉取@Primary bean

使用 @Qualifier 时:应用程序错误 -

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which 
qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=dataSourceA)} 

我尝试向 bean 添加特定名称,例如 @Bean("dataSourceA")、@Bean(name="dataSourceB") 而不是依赖 function 名称和其他一些语法更改,但没有结果。 有人在这里有一些见解吗?

注意:即使一个数据源 bean 被注释掉,而另一个只有一个名称而不是 @primary,它仍然会引发相同的自动装配 bean 错误,所以我不相信注释在我的项目中正常运行(或者我m 用错了)。

您必须创建两个JdbcTemplates ,每个都配置有不同的数据源并使用模板的限定符自动装配,例如

@Bean
@Primary
public JdbcTemplate jdbcTemp1(@Qualifier("dataSourceA") DataSource ds) {
    return new JdbcTemplate(ds);
}

@Bean
public JdbcTemplate jdbcTemp2(@Qualifier("dataSourceB") DataSource ds) {
    return new JdbcTemplate(ds);
}

...

@Autowired
@Qualifier("jdbcTemp2")
private JdbcTemplate jdbcTemplate;

这是因为在@Configuration中定义的 bean 的类型是DataSource并且在@Service中注入JdbcTemplate类型的 bean。

为了让它工作,你应该使用你的两个DataSource创建另外两个JdbcTemplate类型的 bean,如下所示:

@Configuration
public class appDatabaseConfiguration {

 @Bean
 @Primary
 public DataSource dataSourceA() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate corpdwJndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBAProperties>");
 }
 
 @Bean
 public DataSource dataSourceB() throws Exception{
     JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     JndiTemplate jndiTemplate = new JndiTemplate();
     return dataSourceLookup.getDataSource("<DBBProperties>");
 }

 @Bean(name="jdbcTemplateA")
 public JdbcTemplate jdbcTemplateA() {
     return new JdbcTemplate(dataSourceA());
 }

 @Bean(name="jdbcTemplateB")
 public JdbcTemplate jdbcTemplateB() {
     return new JdbcTemplate(dataSourceB());
 }
}

然后,使用@Qualifier("jdbcTemplateA")@Qualifier("jdbcTemplateB")注入您的 bean

暂无
暂无

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

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