Pourquoi NoUniqueBeanDefinitionException: Pas de qualification bean de type est défini: unique correspondant bean, mais à 2
Dans cette petite application, j'ai utilisé @Autowired
annotation avec @Qualifier
annotation pour config les dépendances, mais encore une exception est levée comme mentionné ci-dessous.
Pizaa classe
public class Pizza {
private Address deliverydest;
@Autowired
@Qualifier("ForPizza")
public void setDeliverydest(Address deliverydest) {
this.deliverydest = deliverydest;
}
}
Printemps Contexte De Configuration
<?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>
L'exception levée est
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
.
Maintenant pourquoi c'est le Printemps ne pas considérer le @Qualifier
annotation pour trouver la bonne bean dest1 avoir qualificatif value="ForPizza"
?
- Parce que le
AutowiredAnnotationBeanPostProcessor
ne sais pas à propos de la@Qualifier
annotation. Utilisation<context:annotation-config />
ro si vous voulez vraiment configurer explicitement les analyseurs ajouter unCustomAutowireConfigurer
pour activer la détection de@Qualifier
. - son étrange bcoz j'ai suivi un tutoriel dans ce lien, et il y a travaillé.
Vous devez vous connecter pour publier un commentaire.
Essayez d'ajouter les lignes suivantes à votre Configuration Spring:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Essayez de placer
@Qualifier
sur le paramètre plutôt que sur la méthode:Supprimer toutes les annotations de haricot de post-processeurs et il suffit d'ajouter le ci-dessous en xml.