저는 봄에 새로운 사람입니다. 그러나 기본적인 것들을 시도. 이론에 따르면 나는 일해야한다, 그것은 작동하지 않는다. 나는 다른 id를 가진 2 개의 동일한 콩을 가지고있다. 고유 한 ID @Qualifier("PersonBean2")
인 @Autowired
을 사용할 수 있어야합니다. 하지만 오류가 발생합니다. 나는 무엇이 잘못되었는지 전혀 모른다. 저에게 알려주십시오? 감사!스프링 기본 @Autowired가 작동하지 않는 이유는 무엇입니까?
는 어디 설정 springBeans.xml입니다 : -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<bean id="customer" class="com.mkyong.common.Customer" >
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean1" class="com.mkyong.common.Person">
<property name="name" value="mkyong1" />
<property name="address" value="address 1" />
<property name="age" value="28" />
</bean>
<bean id="PersonBean2" class="com.mkyong.common.Person">
<property name="name" value="mkyong2" />
<property name="address" value="address 2" />
<property name="age" value="28" />
</bean>
</beans>
Customer.java:-
public class Customer {
@Autowired
@Qualifier("PersonBean2")
private Person person;
private int type;
private String action;
}
Person.java:-
public class Person {
private String name;
private String address;
private int age;
}
제거 모든 세터와 게터.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customer': Autowiring of fields failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mkyong.common.Person com.mkyong.common.Customer.person;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mkyong.common.Person] is defined: expected single matching bean but found 2: [PersonBean1, PersonBean2]
스프링에서 '@ Qualifer'를 올바르게 사용했는지 확인하십시오.'javax.inject'가 아니라. –
예, org.springframework.beans.factory.annotation.Qualifier 가져 오기; 맞아요. – masiboo
이름에서 알 수 있듯이'@ Autowired '주석 만 처리하는'AutowiredAnnotationBeanPostProcessor'를 추가했습니다. 프로세서를 사용하는 대신에'@ Qualifier '를 고려한 추가 프로세서를 추가하는' '를 직접 추가하십시오. –