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

2024-01-25

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

这是我的配置类



    @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

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


你的其中一位DataSources必须@Primary (see docs http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-two-datasources).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于 Spring Javaconfig 的按名称自动装配不起作用 的相关文章

随机推荐

  • 无法单击打开文件附件对话框的按钮

    我在用着硒2 我正在尝试单击打开文件附件对话框的按钮 但是当我点击它时什么也没有发生
  • $q 承诺 - 对象不是函数

    我想运行承诺样本https docs angularjs org api ng service q https docs angularjs org api ng service 24q 这是我的代码 angular module test
  • 在所需文件夹中安装 python 包

    我已经下载了一个 python 包安装在我的 ubuntu 机器上 该包已经有一个 setup py 文件可供使用 但我想将默认的 python 安装地址更改为其他地址 专门用于该包 并且不是很好 所以我尝试的是 首先在终端中 我导出新文件
  • RabbitMQ 更改生产系统上的队列参数

    我在面向服务的架构中使用 RabbitMQ 作为消息队列 其中许多单独的 Web 服务发布绑定到 RabbitMQ 队列的消息 这些队列依次被不同的消费者订阅 这些消费者执行后台工作 RabbitMQ 的一个非常普通的用例 现在我想更改一些
  • 如何使用多个VAO和VBO在OpenGL中绘制多个对象?

    我正在尝试使用多个 VAO 和 VBO 在 OpenGL 中渲染多个对象 要使用相同的顶点渲染多个对象 我已经做到了 但我想做的是为每个对象使用不同的顶点 例如绘制一个正方形和一个圆形 对于正方形 我只需要 6 个顶点 但对于圆形 我需要
  • TeamCity 中的 MSBuild 步骤考虑 ASP.NET WEB API 项目的 app.config 而不是 web.config,导致警告 MSB3247

    我有带有 Web conig 文件的 ASP NET WEB API 项目 当它在 VS2015 中构建时 不会报告任何错误和警告 但是 当我使用 MSBuild 构建步骤在 TeamCity 上构建此项目时 我收到警告 MSB3247 R
  • 更改 SQLite3 中的值

    我将首先展示代码 create table products name text primary key price INTEGER insert into table products name price values coke 8 i
  • pom.xml(Maven 多模块项目)中的 在本地工作,但不在公司服务器上工作

    我在用着
  • 由于与 UserInterfaceState.xcuserstate 冲突而无法合并

    我创建了一个分支并做了很多更改 我提交了更改 然后存档了更改 然后我切换到master分支并尝试进行合并 它说我有未提交的更改 所以我在 master 分支上做了一次提交 看看它在说什么 它说有一个名为 UserInterfaceState
  • 扩展 Scala Enumeration.Val 时如何获得正确的值类型

    虽然 S O 上有很多关于 Scala 枚举困难的问题 但我还没有找到解决我的问题的问题 具体来说 我正在尝试将 Planet 示例从Oracle Java 枚举文档 http docs oracle com javase tutorial
  • Primefaces 菜单栏菜单项宽度

    我在使用菜单项时遇到问题 想要附上照片 但不允许所以如果没有它 问题就很愚蠢 我似乎无法更改下拉菜单的宽度 我已经在我的头脑中包含了一个样式 并在其中包含了一个宽度属性 每个菜单项 但它们没有区别 当菜单项突出显示时 选择栏的宽度是正确的
  • 尝试从 MySQL DB 添加视图时出现“命令执行期间遇到致命错误”

    我正在尝试将 MySQL 数据库中的一些表和视图添加到 VS2005 中的项目中 经过大量研究后 我最终使用了 ADO NET 连接器 它工作得很好 除了尝试使用数据源配置向导添加视图时 我收到 命令执行期间遇到致命错误 错误消息 并且当我
  • tableView.tableHeaderView 已设置但未绘制

    每当我设置 tableHeaderView 时 我都不会在模拟器中看到它 如果我将其添加为子视图 它最终会绘制在节标题下方 知道我在这里缺少什么吗 我有一个 XIB 文件 不过 我没有在 IB 中看到任何影响 headerView 的属性
  • 从 JSF 中的托管 bean 获取资源文件的路径

    我遇到这种情况 我试图在从托管 bean 中放置新的头像图像之前删除用户的旧头像图像 String fileName resources img useravatars getSessionBean getSearchAccount get
  • Dagger v2:将 2 个不同的范围注入到一个对象中

    我将 moduleA 设置为应用程序范围的单例提供程序 将 ModuleB 设置为用户相关的对象提供程序 我的用户显示片段将使用系统范围总线向其他人发送消息并使用用户相关对象进行显示 问题是无法将不同的作用域类注入一个对象 使用 compo
  • 动态设置DIV的高度

    在 Web 应用程序中 我有一个页面 其中包含一个 DIV 该 DIV 具有自动宽度 具体取决于浏览器窗口的宽度 我需要对象的自动高度 DIV 从距顶部屏幕约 300 像素的位置开始 其高度应使其延伸到浏览器屏幕的底部 我有容器 DIV 的
  • invoke virtual 总是动态绑定吗?

    今天我的一位朋友告诉我 如果一条指令是invoke virtual 那么它总是动态绑定 而invokespecial总是静态绑定 这是真的吗 如果是 那么为什么当带有final关键字的方法也调用virtual时呢 请澄清我的这个疑问 从本质
  • 基于 APR 的 Apache Tomcat Native 库的不兼容版本 [1.1.34]

    在我的 openSUSE Leap 42 3 服务器上 在嵌入式 Tomcat 服务器上启动 Spring Boot 2 0 0 M6 应用程序期间 我在日志中看到以下错误 ERROR 30471 main o a catalina cor
  • 检查字符是否为数字?

    我需要检查是否justPrices i substr commapos 2 1 该字符串类似于 blabla 120 在本例中 它将检查 0 是否是一个数字 如何才能做到这一点 您可以使用比较运算符来查看它是否在数字字符范围内 var c
  • 基于 Spring Javaconfig 的按名称自动装配不起作用

    我正在尝试使用基于 Javaconfig 的 Spring 配置 我有两个相同类型的 bean 并尝试通过限定符自动装配它们 但它似乎不起作用 这是我的配置类 Configuration EnableAutoConfiguration Co