2016-07-31 98 views
0

我正在研究@Autowired()@Qualifer()以測試對依賴項的自動注入。我創建了引擎類,汽車類。引擎類包含兩個對象e1和e2。當我使用@Qualifier具有值 「E2」,則生成錯誤消息:@Qualifier()和@Autowired()不起作用

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 

用於上述的代碼是:

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:

<?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

嘗試在限定符前放置自動裝配。或更好的,使用@Resource(「e1」) – Sarief

+0

@Sarief我試過但沒有改變輸出 –

+0

你使用的是maven嗎?你能不能發佈你的pom.xml呢? –

回答

0

您必須添加<context:annotation-config/>您spring.xml。這注冊了必要的bean來處理註釋,其中@Qualifier

這是怎麼回事應該看起來像添加了命名空間:

<?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

謝謝@mxf瞭解它 –

+0

@ShaikYakhoobAli很高興爲您提供幫助。我可以請你將這個答案標記爲已接受,如果它解決了你的問題? – mxf

+0

我看不到任何按鈕我可以做 –

0

我,你可能會使用舊Spring庫的印象。我建議你用下面的替換:

org.springframework.core-3.2.5.RELEASE.jar

org.springframework.context-3.2.5.RELEASE.jar

既然你沒有使用Maven來處理你的庫,你必須下載並將它們添加到你的項目。以下鏈接可能會對您有所幫助:Spring ContextSpring Core

順便說一句,你應該使用Maven或Gradle,它會讓你的生活更輕鬆。

+0

我沒有使用3.2.5,但我是使用4.2.6,但我通過添加標籤得到了解決方案 –

相關問題