弹簧@Qualifier注释不起作用

时间:2016-10-30 12:47:50

标签: spring

@Qualifier一起玩@Autowired 这是我的应用程序上下文文件

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

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

<bean id="helloBean" class="com.springex1.HelloWorld1">
    <property name="name" value="Mkyong" />
</bean>

<bean id="address1" class="com.springex1.Address" >
    <property name="street" value="sahajanand" />
</bean>

<bean id="address2" class="com.springex1.Address" >
    <property name="street" value="pune" />
</bean>

这是我的Java课程 HelloWorld1.java

package com.springex1;

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

public class HelloWorld1 {
private String name;

@Autowired
@Qualifier("address1")
private Address address = null; 

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


public void setAddress(Address address) {
    this.address = address;
}

public void printHelloWithAddress() {
    System.out.println("Hello ! " + name + " your street " + address.getStreet());
}


}

Address.java

package com.springex1;

public class Address {
private String street = "";

public void setStreet(String street) {
    this.street = street;
}

public String getStreet(){
    return this.street;
}
}

这是我想要运行的地方 - App.java

package com.springex1;

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

public class App {

public static void main(String[] args) {

    call1("appContext1.xml");           
}

public static void call1(String appContext){
    ApplicationContext context = new ClassPathXmlApplicationContext(appContext);

    HelloWorld1 obj = (HelloWorld1) context.getBean("helloBean");
    obj.printHelloWithAddress();
}     
} 

我一直得到这个例外 - 我不应该因为我的@Qualifier注释带有id&#39; address1&#39;定义 - 所以它不应该抛出异常

  

警告:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为&#39; helloBean的bean时出错&#39;:通过字段&#39;地址&#表达的不满意依赖性39;:没有定义[com.springex1.Address]类型的限定bean:期望的单个匹配bean但找到2:address1,address2;嵌套异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义[com.springex1.Address]类型的限定bean:期望的单个匹配bean但找到2:address1,address2   线程&#34; main&#34;中的例外情况org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称&#39; helloBean创建bean时出错&#39;:通过字段&#39;地址&#39;表示不满意的依赖关系:没有类型为[com.springex1.Address]的限定bean定义:预期的单个匹配bean但找到2:address1,address2;嵌套异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义[com.springex1.Address]类型的限定bean:期望的单个匹配bean但找到2:address1,address2       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)

我正在使用spring 4.3发行版 - 这是我pom中唯一的依赖

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>
<!-- 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>
 -->

3 个答案:

答案 0 :(得分:1)

首先,Spring文档建议不要使用@Qualifier,而不是建议使用@Resource注释按名称注入(如限定符)。

因此,选项一是将@Qualifier替换为@Resource注释。

但是为了使IOC能够使用@Resource正确地进行注射,你应该

名称Address addressAddress address1

现在,如果您仍想使用@Qualifier,则必须将配置更改为:

<bean id="address1" class="com.springex1.Address" >
    <property name="street" value="sahajanand" />
    <qualifier value="address1"/>
</bean>

答案 1 :(得分:0)

您需要将<context:annotation-config />添加到配置xml以解析@Qualifier注释,您可以在下面找到更新的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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

<bean id="helloBean" class="com.code.samples.HelloWorld1">
    <property name="name" value="Mkyong" />
</bean>

<bean id="address1" class="com.code.samples.Address" >
    <property name="street" value="sahajanand" />
</bean>

<bean id="address2" class="com.code.samples.Address" >
    <property name="street" value="pune" />
</bean>
</beans>

答案 2 :(得分:0)

Spring 4.x使用以下bean模式:

<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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

您还需要添加:

<context:annotation-config /> 

或:

<context:component-scan base-package="com.test.package" />

通过添加其中一个标记,您可以从xml中删除AutowiredAnnotationBeanPostProcessor定义。