2014-11-18 5 views
0

저는 봄에 새로운 사람입니다. 그러나 기본적인 것들을 시도. 이론에 따르면 나는 일해야한다, 그것은 작동하지 않는다. 나는 다른 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] 
 
+1

스프링에서 '@ Qualifer'를 올바르게 사용했는지 확인하십시오.'javax.inject'가 아니라. –

+0

예, org.springframework.beans.factory.annotation.Qualifier 가져 오기; 맞아요. – masiboo

+0

이름에서 알 수 있듯이'@ Autowired '주석 만 처리하는'AutowiredAnnotationBeanPostProcessor'를 추가했습니다. 프로세서를 사용하는 대신에'@ Qualifier '를 고려한 추가 프로세서를 추가하는''를 직접 추가하십시오. –

답변

0
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

에서 수입되고 있음을

다시 확인합니다. 프로세서를 직접 사용하는 대신 <context:annotation-config />을 추가하면 추가 프로세서가 추가되어 @Qualifier을 고려합니다.

<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.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:annotation-config /> 

또 다른 팁은 버전없는 스키마 spring-beans.xsd 이상 버전 화 스키마 spring-beans-2.5.xsd을 선호합니다.

0

그것은 작동합니다 - :

테스트

public class App { 
    public static void main(String[] args) { 
    ApplicationContext context = new ClassPathXmlApplicationContext(
      "SpringBeans.xml"); 

    Customer cust = (Customer) context.getBean("customer"); 
    System.out.println(cust); 
    } 
} 

오류 app.java:-! @Qualifier 주석이 예상대로 작동하지 않는 것 같습니다. @Qualifier 주석이 패키지 org.springframework.beans.factory.annotation 당신은 이름에서 알 수 있듯이, 단지 @Autowired 주석 처리는 AutowiredAnnotationBeanPostProcessor을 추가 한

+0

예가 org.springframework.beans.factory.annotation.Qualifier에서 가져 왔습니다. – masiboo

관련 문제