@Qualifier()和@Autowired()无法正常工作

时间:2016-07-31 13:07:00

标签: java xml spring spring-mvc

我正在使用@Autowired()@Qualifer()来测试依赖项的自动注入。我创建了Engine Class,Car class。引擎类包含两个对象e1和e2。当我使用值为“e2”的@Qualifier时,它会生成错误消息:

    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();
  }
  }

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我认为您可能正在使用旧的Spring库。我建议您应该用以下内容替换它:

  

org.springframework.core-3.2.5.RELEASE.jar

     

org.springframework.context-3.2.5.RELEASE.jar

由于您未使用Maven来处理库,因此必须下载并将其添加到项目中。以下链接可能对您有所帮助:Spring ContextSpring Core

顺便说一句,你应该使用Maven或Gradle,它会让你的生活更轻松。