基于名称的基于Spring Javaconfig的自动装配不起作用 [英] Spring Javaconfig based autowire by name not working

查看:82
本文介绍了基于名称的基于Spring Javaconfig的自动装配不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的配置类




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

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

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

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

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).

推荐答案

您的DataSources之一必须是@Primary(请参阅

One of your DataSources has to be @Primary (see docs).

这篇关于基于名称的基于Spring Javaconfig的自动装配不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆