2014-06-26 31 views
2

在這個小應用程序,我已經使用@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"

+1

因爲'AutowiredAnnotationBeanPostProcessor'不知道了'@ Qualifier'註釋。使用''ro如果你真的想明確地配置解析器,添加一個'CustomAutowireConfigurer'來啓用'@ Qualifier'的檢測。 –

+0

@Deinum,它的奇怪的bcoz我正在關注這個[link](http://youtu.be/IVIhVJJGo68)中的教程,它在那裏工作。 –

回答

3

嘗試添加下列到你的Spring配置:

<context:annotation-config/> 
<context:component-scan base-package="com.test.Shopping"/> 
+0

不!同樣的例外。無論如何,我以前已經添加annotation.AutowiredAnnotationBeanPostProcessor –

+0

此編輯後,現在異常已更改爲'由於:org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有發現依賴性的[com.test.Shopping.Address]類型的限定bean:預計至少有1個bean有資格作爲這個依賴關係的autowire候選人。依賴註釋:{}' –

+0

對不起,我的錯誤實際上都起作用了。在配置中有一個小的錯字 –

1

嘗試把@Qualifier的參數,而不是方法:

@Autowired 
public void setDeliverydest(@Qualifier("ForPizza") Address deliverydest) { ... } 
相關問題