Spring Boot忽略@Qualifier注解

5

我正在将一款活跃的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配置中没有任何限定符。 - Stephane Nicoll
id作为限定符名称。 - Ronco
我不知道为什么这对你不起作用,但请注意,如果bean名称与变量名称匹配,您甚至不需要限定符。也就是说,@Autowired BasicDataSource datasource4 将会与名为 datasource4 的 bean 进行自动装配。 - slim
请提供一个 [mcve]。 - Sotirios Delimanolis
@Thanos,你说得对,我已经将ID从原来的名称更改为简化示例,可能是我错过了...我已经更新了它。 - Ronco
显示剩余3条评论
2个回答

4

当我使用多个数据源时,遇到了相同的错误。将其中一个数据源bean设置为“primary”后,问题似乎得到解决,然后我就能够使用@Qualifier注释来自动装配正确的bean。

Spring Boot文档here也支持这一点,它说在使用多个数据源时,必须有一个数据源bean是primary。

我使用@Bean注释在Java中配置了我的bean,因此我使用@Primary注释使一个bean成为主要的。但是,如果您在XML中进行配置,则XML中的元素似乎具有可选的“primary”属性,可以在配置bean时使用。


-1
正如Jake所说,应该至少添加@Primary注释以消除此错误。另外:
如果您从外部文件获取数据源属性,请使用spring.datasource开头的主要数据源属性(即url、user、pass等),以覆盖Spring Boot的默认内部数据源,该数据源是Derby、HSQL或其他,具体取决于您的配置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接