当前位置: 首页 > 知识库问答 >
问题:

NouniqueBeanDefinitionException:没有“Beans.Engin”类型的合格bean可用:需要一个匹配bean,但找到2个

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

<beans>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="def" class="beans.Engine">
    <property name="modelYear" value="2015"/>
</bean>

<bean id="abc" class="beans.Engine">
    <property name="modelYear" value="2015"/>
</bean>
<bean id="c" class="beans.Car"/>    
</beans>
@Autowired
@Qualifier(value="def")
private Engine engine;

public void printCarData(){
        System.out.println("Engine Model Year  :  "+engine.getModelYear());

}
private String modelYear;

public void setModelYear(String modelYear) {
    this.modelYear=modelYear;
}

public String getModelYear() {
    return modelYear;
}
public static void main(String [] a){       

    ApplicationContext ap = new ClassPathXmlApplicationContext("resources/spring.xml");

    Car c = (Car)ap.getBean("c");
    c.printCarData();
}

}

共有1个答案

贺桐
2023-03-14
<bean id="def" class="beans.Engine">
    <property name="modelYear" value="2015"/>
</bean>

<bean id="abc" class="beans.Engine">
    <property name="modelYear" value="2015"/>
</bean>
 类似资料: