2016-07-31 39 views
0

Ich arbeite an @Autowired() und @Qualifer(), um automatische Injektionen auf eine Abhängigkeit zu testen. Ich habe Motor Class, Car-Klasse erstellt. Die Engine-Klasse enthält zwei Objekte e1 und e2. Als ich @Qualifier mit dem Wert "e2" verwenden, wird die Erzeugung der Fehlermeldung:@Qualifier() und @Autowired() funktioniert nicht

WARNING: Exception encountered during context initialization -  
    cancelling refresh attempt: 
    org.springframework.beans.factory.BeanCreationException: Error creating 
    bean with name 'c': Injection of autowired dependencies failed; nested 
    exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.abc.xyz.Engine 
    com.abc.xyz.Car.engine; nested exception is 
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No 
    qualifying bean of type [com.abc.xyz.Engine] is defined: expected single 
     matching bean but found 2: e1,e2 

    Exception in thread "main" 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'c': Injection of autowired dependencies failed; nested 
exception is org.springframework.beans.factory.BeanCreationException: Could 
    not autowire field: private com.abc.xyz.Engine com.abc.xyz.Car.engine; 
    nested exception is 
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No 
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single 
matching bean but found 2: e1,e2 
at 

    org.springframework.beans.factory.annotation. 
    AutowiredAnnotationBeanPostProcessor 
    .postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
at org.springframework.beans.factory.support. 
    bstractAutowireCapableBeanFactory.populateBean 
    (AbstractAutowireCapableBeanFactory.java:1214) 
at org.springframework.beans.factory.support. 
    AbstractAutowireCapableBeanFactory.doCreateBean 
    (AbstractAutowireCapableBeanFactory.java:543) 
at org.springframework.beans.factory.support. 
    AbstractAutowireCapableBeanFactory.createBean 
    (AbstractAutowireCapableBeanFactory.java:482) 
at org.springframework.beans.factory.support. 
    AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
at org.springframework.beans.factory.support. 
    DefaultSingletonBeanRegistry.getSingleton 
    (DefaultSingletonBeanRegistry.java:230) 
at org.springframework.beans.factory.support. 
    AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support. 
     AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
at org.springframework.beans.factory.support. 
    DefaultListableBeanFactory.preInstantiateSingletons 
    (DefaultListableBeanFactory.java:772) 
at org.springframework.context.support. 
     AbstractApplicationContext.finishBeanFactoryInitialization 
    (AbstractApplicationContext.java:839) 
at org.springframework.context.support. 
     AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
at org.springframework.context.support. 
     ClassPathXmlApplicationContext.<init> 
    (ClassPathXmlApplicationContext.java:139) 
at org.springframework.context.support. 
    ClassPathXmlApplicationContext.<init> 
    (ClassPathXmlApplicationContext.java:83) 
at com.abc.xyz.ClientApp.main(ClientApp.java:9) 
    Caused by: org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.abc.xyz.Engine 
    com.abc.xyz.Car.engine; nested exception is 
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No 
    qualifying bean of type [com.abc.xyz.Engine] is defined: expected single 
    matching bean but found 2: e1,e2 
at org.springframework.beans.factory.annotation. 
    AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject 
    (AutowiredAnnotationBeanPostProcessor.java:573) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject 

    (InjectionMetadata.java:88) 
at org.springframework.beans.factory.annotation. 
    AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues 
    (AutowiredAnnotationBeanPostProcessor.java:331) 
... 13 more 

    Caused by: org.springframework.beans.factory. 
    NoUniqueBeanDefinitionException: No qualifying bean of type 
    [com.abc.xyz.Engine] is defined: expected single matching 
    bean but found 2: e1,e2 
at 
    org.springframework.beans.factory. 
    support.DefaultListableBeanFactory.doResolveDependency 
    (DefaultListableBeanFactory.java:1126) 
at org.springframework.beans.factory. 
    support.DefaultListableBeanFactory.resolveDependency 
    (DefaultListableBeanFactory.java:1014) 
at org.springframework.beans.factory. 
    annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement. 
    inject(AutowiredAnnotationBeanPostProcessor.java:545) 
... 15 more 

Der Code für das oben ist:

Engine.java:

 package com.abc.xyz; 

    public class Engine { 

    private String name; 

     public String getName() { 
     return name; 
    } 

public void setName(String name) { 
    this.name = name; 
} 

    } 

Car.java :

package com.abc.xyz; 


    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.beans.factory.annotation.Qualifier; 

    public class Car { 

@Qualifier(value="e1") 
@Autowired() 
private Engine engine; 

     public void display() 
     { 

System.out.println("Car Engine="+engine.getName()); 
     } 

     } 

spring.xml:

012.351.
<?xml version="1.0" encoding="UTF-8"?> 

     <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-3.0.xsd"> 

    <beanclass="org.springframework.beans.factory.annotation. 
    AutowiredAnnotationBeanPostProcessor"> 

    </bean> 
    <bean id="e1" class="com.abc.xyz.Engine"> 

      <property name="name" value="2015"></property> 

    </bean>  

     <bean id="e2" class="com.abc.xyz.Engine"> 

      <property name="name" value="2016"></property> 

     </bean> 


     <bean id="c" class="com.abc.xyz.Car"> 
     </bean> 
</beans> 

ClientApp.java:

 package com.abc.xyz; 

     import org.springframework.context.ApplicationContext; 
     import org.springframework.context.support.ClassPathXmlApplicationContext; 

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

    Car c=(Car)context.getBean("c"); 
    c.display(); 
    } 
    } 
+0

versuchen, vor Qualifier autowired zu platzieren. oder besser, verwenden Sie @Resource ("e1") – Sarief

+0

@Sarief Ich habe versucht, aber keine Änderung in der Ausgabe –

+0

Verwenden Sie Maven? Könnten Sie bitte auch Ihre pom.xml posten? –

Antwort

0

Sie haben <context:annotation-config/> zu Ihrem spring.xml hinzuzufügen. Dies registriert notwendige Beans, um Anmerkungen zu verarbeiten, und @Qualifier unter ihnen. Diese

ist, wie es sein sollte wie mit zusätzlichem Namensraum aussieht:

<?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.xsd"> 

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

<context:annotation-config/> 

<bean id="e1" class="com.abc.xyz.Engine"> 
    <property name="name" value="2015"/> 
</bean> 

<bean id="e2" class="com.abc.xyz.Engine"> 
    <property name="name" value="2016"/> 
</bean> 

<bean id="c" class="com.abc.xyz.Car"/> 
</beans> 
+0

Dank @mxf bekam es –

+0

@ShaikYakhoobAli Froh, hilfreich zu sein. Darf ich Sie bitten, diese Antwort als akzeptiert zu markieren, wenn sie Ihr Problem behoben hat? – mxf

+0

Ich sehe keine Schaltfläche akzeptiert, die ich tun kann –

0

ich unter dem Eindruck bin, dass Sie vielleicht die alte Spring Bibliothek werden. Ich schlage vor, dass Sie es mit den folgenden ersetzen sollte:

org.springframework.core-3.2.5.RELEASE.jar

org.springframework.context-3.2.5.RELEASE.jar

Da Sie Maven nicht zur Verwaltung Ihrer Bibliotheken verwenden, müssen Sie sie herunterladen und zu Ihrem Projekt hinzufügen. Die folgenden Links könnten Ihnen helfen: Spring Context und Spring Core.

BTW, sollten Sie Maven oder Gradle verwenden, wird es Ihnen das Leben leichter machen.

+0

Ich benutze nicht 3.2.5, aber ich bin mit 4.2.6, aber ich habe die Lösung durch Hinzufügen Tag –

Verwandte Themen