java - @Qualifier() 和 @Autowired() 不起作用

标签 java xml spring spring-mvc

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

    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

上面的代码是:

引擎.java:

     package com.abc.xyz;

     public class Engine {

    private String name;

      public String getName() {
      return name;
    }

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

     }

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

最佳答案

您必须添加<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>

关于java - @Qualifier() 和 @Autowired() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684278/

相关文章:

java - 我们什么时候会在 Spring 中使用 applicationContext.xml?

java - Spring session 碰撞 session

java - 哪些部分或包可以从 JDK 中删除?

java - RMI 安全管理器

xml - Groovy 中的 UnEscape Xml

c# - 字符串到双转换(逗号到点的问题)

xml - 使用 XML 模式作为 OO 继承 (JAXB) - 可能吗?

spring - Spring Boot 中的 Dispatcher Servlet

java - 运算符(operator)不使用 If 语句和扫描器

java - 在 paint() 中调用 repaint() 会导致无限递归吗?