一尘不染

基于名称的基于Spring Javaconfig的自动装配不起作用

spring-boot

我正在尝试使用基于Javaconfig的Spring配置。我有两个相同类型的bean,并尝试通过Qualifier自动装配它们。但这似乎没有用。

这是我的配置类

    @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;
        }
    }

这两个是我尝试使用自动装配的其他类。

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

但是,当我通过ConfigurableApplicationContext context = SpringApplication.run(BasicConfig.class)它启动我的应用程序时,会引发异常。

    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

我不知道了 我试图用谷歌搜索,但似乎所有人都建议添加Qualifier。但是我已经添加了,但是没有用。请帮忙。我正在使用Spring 4(当前版本)。


阅读 383

收藏
2020-05-30

共1个答案

一尘不染

DataSources必须之一@Primary(请参阅docs)。

2020-05-30