当前位置: 首页 > 知识库问答 >
问题:

Spring-Boot忽略@限定符注释

吴高远
2023-03-14

我正在迁移一个活动的Spring Web应用到Spring Boot(1.4.2)。

这些bean是在XML中定义的,因为它正在加载@重要资源。

我正在启动的4个bean是同一个对象BasicDataSource的实例。

为了告诉Spring要加载哪个,我为每个都设置了一个ID,并使用@Qualifer将正确的bean绑定到变量。

但Spring似乎忽略了我的@Qualifier,并抛出“没有可用的类型为'javax.sql.DataSource'的限定bean:预期只有一个匹配bean,但找到了4个:DataSource1、DataSource2、DataSource3、DataSource4”

P. S-请注意,具有@Qualifer的类是一个抽象类,未能实例化的类是一个扩展类,但是@Qualifer具有@继承。

XML

<beans
    xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   http://www.springframework.org/schema/jee 
   http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
   http://www.springframework.org/schema/task 
   http://www.springframework.org/schema/task/spring-task-3.2.xsd
   http://www.springframework.org/schema/cache
   http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<context:load-time-weaver aspectj-weaving="on"/>
<cache:annotation-driven mode="aspectj"/>
<context:property-override location="file:/app/config/dataSourceOverride.cfg"/>
<context:annotation-config />

<bean id="DataSource1" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="userId" value="4" />
</bean>

<bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="userId" value="3" />
</bean>

<bean id="DataSource3" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="userId" value="2" />
</bean>

<bean id="DataSource4" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="userId" value="1" />
</bean>

SpringBoot应用程序主

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.app")
@ImportResource("com/app/startup/spring.xml")
public class SpringBootServer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootServer.class, args);

    }
}

抽象类

public abstract class GenericDao {

    public GenericDao() {

    }

    private Logger logger = LoggerFactory.getLogger(GenericDao.class);

    @Autowired
    @Qualifier("DataSource1")
    protected BasicDataSource dataSource1Impl;

    @Autowired
    @Qualifier("DataSource2")
    protected BasicDataSource dataSource2Impl;

    @Autowired
    @Qualifier("DataSource3")
    protected BasicDataSource dataSource3Impl;

    @Autowired
    @Qualifier("DataSource4")
    protected BasicDataSource dataSource4Impl;
}

固体钙

@Component("widgetsDao")
public class WidgetsDao extends GenericDao {

##Some methods##

}

例外

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dataSource1Impl in com.app.dal.GenericDao required a single bean, but 4 were found:
    - DataSource1: defined in class path resource [com/app/startup/app-spring.xml]
    - DataSource2: defined in class path resource [com/app/startup/app-spring.xml]
    - DataSource3: defined in class path resource [com/app/startup/app-spring.xml]
    - DataSource4: defined in class path resource [com/app/startup/app-spring.xml]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

什么可能使spring忽略我的@Qualifier注释??

感谢前方。

共有2个答案

微生承业
2023-03-14

正如Jake所说,至少应该添加@Primary注释以消除此错误。加上:

如果您从外部文件获取数据源属性,请使用spring.datasource启动主数据源的属性(即url、user、Pass等),以便覆盖Spring Boot的默认内部数据源,即Derby、HSQL等,具体取决于你的配置。

宁兴修
2023-03-14

在使用多个数据源时,我看到了相同的错误,在将其中一个数据源bean设置为“主要”之后,问题似乎得到了解决,然后我就能够使用@Qualifier注释自动连接正确的bean。

这里的Spring Boot文档也同意这一点,即当使用多个数据源时,其中一个数据源bean必须是主数据源。

我使用@Bean注释在Java中配置了Bean,因此我使用@Primary注释生成了一个主注释。但是,如果您是在XML中配置的,那么XML中的元素看起来确实有一个可选的“primary”属性,在XML中配置bean时可以使用该属性。

 类似资料:
  • 我有一个具有依赖项的Spring Boot应用程序。我的实体类有一个带有列名的列注释。例如: 由此生成的SQL将创建为列名。在寻找解决方案之后,我发现解决了这个问题(列名取自列注释)。 但是,我的问题是为什么没有将naming_strategy设置为,JPA忽略列注释?也许冬眠方言与此有关?我正在连接到MS SQL 2014 Express,我的日志包含:

  • 有什么想法为什么@primary在这里没有被考虑在内吗?

  • 我有2个Spring Boot(1.4.1-发行版)控制台应用程序使用了Logback。这两个配置文件或多或少是相同的,都位于my/src/main/resources文件夹中,名为logback-spring.xml。 这两个项目都在其pom.xml中包含了maven依赖项spring-boot-starter-logging,并获取1.1.7版本的日志。 两个POM中定义的Spring Boo

  • 问题内容: 我的@Transactionnal注释似乎被忽略了。我对Spring容器的初始化没有任何错误。看来我的方法尚未被Spring TX框架代理。在执行服务的方法期间,JDBCTemplate会引发预期的RuntimeException。问题在于JDBC连接没有回滚,并且更改保持不变。stacktrace没有显示应该包装我的服务方法的代理的任何迹象。 编辑:添加了控制器的代码 编辑2:添加了

  • 我有一个计算着色器,可以使用读取带符号的标准化整数图像。 图像本身(包含正值和负值)被创建为,并由先前gpass中的片段着色器写入。绑定到计算着色器中descriptorsetlayout绑定的imageview也使用相同的格式创建。 一切正常。 昨天我意识到在计算着色器中我使用了错误的图像格式限定符。有点困惑(我不明白它如何正确读取无符号规范化值)我更正为,并且...什么都没变。 我做了几次测试

  • 在我们的一个项目中,我们遇到了一个问题,Spring忽略了事务注释,然后失败了,出现了以下错误。 启动ApplicationContext时出错。要显示条件报告,请在启用“调试”的情况下重新运行应用程序。2018-09-13 15:05:18406错误[主]组织。springframework。靴子SpringApplication应用程序运行失败组织。springframework。道。Inva