I am trying to use Javaconfig based Spring configurations. I have two beans of same type and trying to autowire them by Qualifier. But it does not seems to working.
This is my Configuration class
@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;
}
}
This two are my other classes where I am trying to use autowire them.
@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); } }
But when I start my application via ConfigurableApplicationContext context = SpringApplication.run(BasicConfig.class)
it throws an 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
I am out of idea. I tried to google it but all seems to suggest adding Qualifier. But I have already add that but with no use. Please help. I am using Spring 4(current version).
One of your DataSources
has to be @Primary
(see docs).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With