简体   繁体   English

@Qualifier在春季5.0.6中不起作用

[英]@Qualifier not working in spring 5.0.6

@Qualifier("id") is now working. @Qualifier(“ id”)现在正在工作。 It is showing 它显示

No qualifying bean of type 'beans.Engine_AutoAnno' available: expected single matching bean but found 2: e,e1 没有可用的'beans.Engine_AutoAnno'类型的合格Bean:预期为单个匹配的Bean,但找到了2:e,e1

This is my Engine_AutoAnno.java 这是我的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;
    }
}

This is my Car_AutoAnno.java 这是我的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());
    }
}

This is my main class Client_autoAnno.java 这是我的主类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();

    }
}

This is my XML file: 这是我的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>

This is the exception i am getting: 这是我得到的例外:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'c': Unsatisfied dependency expressed through field 'engine'; 线程“主”中的异常org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ c”的bean时出错:通过字段“ 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 嵌套的异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为'beans.Engine_AutoAnno'的合格bean:期望的单个匹配bean,但找到2:e,e1

How to solve this? 如何解决呢?

Thanks in avdance. 在此感谢您。

Add <context:annotation-config /> in your xml @Qualifier annotations need this. 在XML @Qualifier注释中添加<context:annotation-config />需要此功能。

<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.

The answer given by Mr. Raghvendra Garg 拉格文德拉·加尔格先生的回答

worked fine... 工作正常...

This is the updated XML file.. 这是更新的XML文件。

<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-3.0.xsd">


    <!-- activate autowire annotation -->
    <context:annotation-config />
    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM