我正在开发一个基于Spring Boot的应用程序,我想在其中创建2个bean:一个将指向'Oracle'数据库; 另一个将指向Hive.我已将它们声明如下:
public @Bean
BoneCPDataSource metadataDataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setDriverClass(getDriver());
boneCPDataSource.setJdbcUrl(getJdbcUrl());
boneCPDataSource.setUser(getUser());
boneCPDataSource.setPassword(getPassword());
boneCPDataSource.setMaxConnectionsPerPartition(5);
boneCPDataSource.setPartitionCount(5);
return boneCPDataSource;
}
public @Bean
BasicDataSource hiveDataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
// Note: In a separate command window, use port forwarding like this:
//
// ssh -L 127.0.0.1:9996:<server>:<port> -l <userid> <server>
//
// and then login as the generic user.
basicDataSource.setDriverClassName("org.apache.hadoop.hive.jdbc.HiveDriver");
basicDataSource.setUrl("jdbc:hive://127.0.0.1:9996:10000/mytable");
return basicDataSource;
}
Run Code Online (Sandbox Code Playgroud)
问题是在启动时我得到了这个:
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private javax.sql.DataSource
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.dataSource;
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [javax.sql.DataSource] is defined: expected
single matching bean but found 2: metadataDataSource,hiveDataSource
Run Code Online (Sandbox Code Playgroud)
主要是因为它们都继承自javax.sql.DataSource.解决这个问题的最佳方法是什么?
编辑:
现在我已经宣布如下:
public @Bean (name="metadataDataSource")
BoneCPDataSource metadataDataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setDriverClass(getDriver());
boneCPDataSource.setJdbcUrl(getJdbcUrl());
boneCPDataSource.setUser(getUser());
boneCPDataSource.setPassword(getPassword());
boneCPDataSource.setMaxConnectionsPerPartition(5);
boneCPDataSource.setPartitionCount(5);
return boneCPDataSource;
}
public @Bean (name="hiveDataSource")
BasicDataSource hiveDataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
// Note: In a separate command window, use port forwarding like this:
//
// ssh -L 127.0.0.1:9996:<server>:<port> -l <userid> <server>
//
// and then login as the generic user.
basicDataSource.setDriverClassName("org.apache.hadoop.hive.jdbc.HiveDriver");
basicDataSource.setUrl("jdbc:hive://127.0.0.1:9996:10000/mytable");
return basicDataSource;
}
Run Code Online (Sandbox Code Playgroud)
得到了这个例外:
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private javax.sql.DataSource
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.dataSource;
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [javax.sql.DataSource] is defined: expected
single matching bean but found 2: metadataDataSource,hiveDataSource
Run Code Online (Sandbox Code Playgroud)
其他类引用这些bean如下:
公共类MetadataProcessorImpl实现MetadataProcessor {
@Autowired
@Qualifier("metadataDataSource")
BoneCPDataSource metadataDataSource;
Run Code Online (Sandbox Code Playgroud)
@Controller public class HiveController {
@Autowired
@Qualifier("hiveDataSource")
BasicDataSource hiveDataSource;
Run Code Online (Sandbox Code Playgroud)
小智 18
将其中一个梁设置为@Primary,如67.2配置两个数据源一节中所述
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-two-datasources
Lui*_*oza 12
使用时为您的bean指定不同的名称@Bean
:
@Bean(name="bonecpDS")
public BoneCPDataSource metadataDataSource() {
//...
}
@Bean(name="hiveDS")
public BasicDataSource hiveDataSource() {
//...
}
Run Code Online (Sandbox Code Playgroud)
然后,在注入bean时,使用@Qualifier
并指定bean的名称:
@Component
public class FooComponent {
@Autowired
@Qualifier("bonecpDS")
DataSource boneCPDataSource;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您想同时使用两个数据源并且它们没有主次之分,您应该DataSourceAutoConfiguration
在您的应用程序上禁用@SpringBootApplication(excludes = {DataSourceAutoConfiguration.class})
.
既然DataSourceAutoConfiguration
会开始上课DataSourceInitializer
。类中的 init 方法DataSourceInitializer
需要 get DataSource
。当存在多个时DataSource
,系统会因获取哪个而感到困惑DataSource
。
@SpringBootApplication(excludes = {DataSourceAutoConfiguration.class})
意味着系统在运行应用程序时不会加载DataSourceAutoConfiguration.class
。
归档时间: |
|
查看次数: |
26913 次 |
最近记录: |