[英]Spring-Boot ignores @Qualifier annotation
我正在將一個活躍的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”
PS - 請注意,具有@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啟動應用程序主要
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;
}
堅實的Calss
@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注釋?
謝謝你。
我在使用多個數據源時看到同樣的錯誤,並且在使一個數據源bean“主要”之后,問題似乎得到了解決,然后我就能夠使用@Qualifier注釋自動裝配正確的bean。
春季啟動文檔這里與贊同,他說,使用多個數據源時,數據源豆之一必須是首要。
我使用@Bean注釋在Java中配置我的bean,因此我使用@Primary注釋來創建一個主要注釋。 但是,如果您在XML中進行配置,則看起來XML中的元素確實具有可選的“主要”屬性,如果在XML中配置Bean,則可以使用該屬性。
正如Jake所說,至少應該添加@Primary
注釋來擺脫這個錯誤。 加:
如果要從外部文件中獲取數據源屬性,請使用spring.datasource
啟動主數據源的屬性(即url,user,pass等),以覆蓋Spring Boot的默認內部數據源,即Derby,HSQL,具體取決於您的配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.