在這個小應用程序,我已經使用@Autowired
批註與@Qualifier
註釋一起配置的依賴性,但仍低於提到的異常被拋出。爲什麼NoUniqueBeanDefinitionException:無類型的符合條件的bean的定義:預期單個匹配的bean,但發現2
Pizaa類
public class Pizza {
private Address deliverydest;
@Autowired
@Qualifier("ForPizza")
public void setDeliverydest(Address deliverydest) {
this.deliverydest = deliverydest;
}
}
Spring上下文配置
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="pizza" class="com.test.Shopping.Pizza" />
<bean id="Cust1Address" class="com.test.Shopping.Address" />
<bean id="dest1" class="com.test.Shopping.Address" >
<qualifier value="ForPizza" />
<property name="buildingno" value="flat1/door2" />
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>
引發的異常是
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pizza': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.Shopping.Pizza.setDeliverydest(com.test.Shopping.Address); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.Shopping.Pizza.setDeliverydest(com.test.Shopping.Address); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1
。
現在爲什麼春季不考慮@Qualifier
註釋找到正確的豆dest1有限定符value="ForPizza"
?
因爲'AutowiredAnnotationBeanPostProcessor'不知道了'@ Qualifier'註釋。使用' 'ro如果你真的想明確地配置解析器,添加一個'CustomAutowireConfigurer'來啓用'@ Qualifier'的檢測。 –
@Deinum,它的奇怪的bcoz我正在關注這個[link](http://youtu.be/IVIhVJJGo68)中的教程,它在那裏工作。 –