【问题标题】:Spring-Boot ignores @Qualifier annotationSpring-Boot 忽略 @Qualifier 注释
【发布时间】:2017-05-04 18:26:18
【问题描述】:

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

bean 在使用 @ImportResource 加载时在 XML 中定义。

我开始的 4 个 bean 是同一对象 BasicDataSource 的一个实例。

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

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

P.S - 请注意,具有 @Qualifier 的类是抽象类,无法实例化的类是扩展类,但 @Qualifier 具有 @Inherited。

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>

Spring Boot 应用主程序

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 注释??

谢谢。

【问题讨论】:

  • 您的 XML 配置中没有限定符。
  • id 用作限定符名称
  • 我不知道为什么这对您不起作用,但请注意,如果 bean 名称与变量名称匹配,您甚至不需要限定符。也就是说,@Autowired BasicDataSource datasource4 将连接到一个名为 datasource4 的 bean。
  • @Sotirios Delimanolis 更新为最小、完整和可验证的示例

标签: java spring spring-boot autowired qualifiers


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-06
      • 2018-07-07
      • 1970-01-01
      • 2012-05-19
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2015-04-14
      相关资源
      最近更新 更多