java - @Qualifier 在 spring 5.0.6 中不起作用

标签 java spring dependency-injection annotations autowired

@Qualifier("id") 正在工作。正在显示

No qualifying bean of type 'beans.Engine_AutoAnno' available: expected single matching bean but found 2: e,e1

这是我的Engine_AutoAnno.java

package beans;
public class Engine_AutoAnno {

    private String model;

    public void setModel(String model) {
        this.model = model;
    }
    public String getModel() {
        return model;
    }
}

这是我的 Car_AutoAnno.java

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

public class Car_AutoAnno {

    @Autowired
    @Qualifier("e")
    private Engine_AutoAnno engine;

    public void printCar() {

        System.out.println("Car engine: "+engine.getModel());
    }
}

这是我的主类Client_autoAnno.java

package testMain;

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

import beans.Car_AutoAnno;

public class Client_autoAnno {

    public static void main(String[] args) {

        ApplicationContext app = new ClassPathXmlApplicationContext("resource/spring_autowireAnno.xml");

        Car_AutoAnno car = (Car_AutoAnno) app.getBean("c");

        car.printCar();

    }
}

这是我的 XML 文件:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

    <!-- activate autowire annotation -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    <bean id="c" class="beans.Car_AutoAnno"/>

    <bean id="e" class="beans.Engine_AutoAnno">
        <property name="model" value="XF"/>
    </bean>
    <bean id="e1" class="beans.Engine_AutoAnno">
        <property name="model" value="XJ"/>
    </bean>
</beans>

这是我遇到的异常:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'c': Unsatisfied dependency expressed through field 'engine'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'beans.Engine_AutoAnno' available: expected single matching bean but found 2: e,e1

如何解决这个问题?

非常感谢。

最佳答案

添加<context:annotation-config />在你的 xml @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" xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
// your bean definations here.

关于java - @Qualifier 在 spring 5.0.6 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51035242/

相关文章:

java - 参数中的 NdefRecord.createMime 错误

spring - Spring Boot与MySQL中的多个架构连接

java - Spring Boot Actuator - 如何向/关闭端点添加自定义逻辑

c# - 是否保证多个 ninject 绑定(bind)保持其绑定(bind)顺序

Angular APP_INITIALIZER 提供程序将一项注入(inject)服务视为未定义,但其他服务注入(inject)得很好

Java传递变量——向下转型

java - 如何将文本文件中的变量逐行分配到java文件中

java - 单元测试类的最佳实践,主要负责调用依赖项的方法,但也包含逻辑

java - 如何优雅地停止嵌入式 Tomcat 并且只有在当前处理请求必须完成后才能停止 tomcat

javascript - Aurelia js 添加 npm 包