@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/