Spring Javaconfig basiert autowire by name funktioniert nicht
Ich versuche, mit Javaconfig-basierte Spring-Konfigurationen. Ich habe zwei Bohnen der gleichen Art und versuchen, autowire Sie durch die Qualifier. Aber es scheint nicht zu arbeiten.
Dies ist meine Konfiguration, Klasse
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.test")
public class BasicConfig {
@Bean(name = "mysqlSource")
@Qualifier("mysqlSource")
public DataSource jdbcTemplateMySql() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8");
dataSource.setUsername(mysqlUser);
dataSource.setPassword(mysqlPass);
return dataSource;
}
@Bean(name = "oracleSource")
@Qualifier("oracleSource")
public DataSource jdbcSourceOracle() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@lab-scan.tigeritbd.com:1521/evidb.tigeritbd.com");
dataSource.setUsername(oracleUser);
dataSource.setPassword(oraclePass);
return dataSource;
}
}
Dies sind meine beiden anderen Klassen, wo ich bin versucht, autowire Sie.
@Repository public class TrackingInfiniDBRepo implements DataPutRepo { private NamedParameterJdbcTemplate jdbcTemplate; @Autowired void setJdbcTemplateOracle(@Qualifier("mysqlSource") DataSource dataSource) { jdbcTemplate = new NamedParameterJdbcTemplate(dataSource); } }
@Repository public class OracleDataFetcherRepo implements DataFetcherRepo { private NamedParameterJdbcTemplate jdbcTemplateOracle; @Autowired void setJdbcTemplateOracle(@Qualifier("oracleSource") DataSource dataSource) { jdbcTemplateOracle = new NamedParameterJdbcTemplate(dataSource); } }
Aber wenn ich anfange meine Bewerbung per ConfigurableApplicationContext context = SpringApplication.run(BasicConfig.class)
wirft er eine exception.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.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: mysqlSource,oracleSource
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
at com.tigerit.tracking.Application.main(Application.java:21)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.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: mysqlSource,oracleSource
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 15 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: mysqlSource,oracleSource
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:970)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 17 more
Ich bin aus der Idee. Ich habe versucht zu google, aber alle scheint nahe zu legen, hinzufügen Qualifier. Aber ich habe schon hinzufügen, dass aber ohne Einsatz. Bitte helfen Sie. Ich bin mit Spring 4(aktuelle version).
- Spring Boot erwartet, aufgrund der Abwesenheit eines
JdbcTemplate
, dass es eine einzige Datenquelle, so dass eine automatisch konfiguriertJdbcTemplate
Hinzugefügt werden können. Allerdings hast du 2 Datenquellen. In der@ EnableAutoConfiguration
fügen Sie eine exclude-fürDataSourceAutoConfiguration
.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Einer Ihrer
DataSources
werden@Primary
(siehe docs).@Primary
erforderlich ist, aber es scheint, dass es ist.