【问题标题】:Springs @Qualifier annotation not workingSprings @Qualifier 注释不起作用
【发布时间】:2016-10-30 12:47:50
【问题描述】:

我在玩 @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());
}


}

地址.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();
}     
} 

我不断收到此异常 - 理想情况下我不应该,因为我定义了 id 'address1' 的@Qualifier 注释 - 所以它不应该抛出异常

警告:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“helloBean”的 bean 时出错:通过字段“地址”表示不满足的依赖关系:没有符合条件的 bean 类型 [com .springex1.Address] 已定义:预期单个匹配 bean 但找到 2:address1,address2;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义 [com.springex1.Address] 类型的合格 bean:预期单个匹配 bean,但找到 2:address1,address2 线程“主”org.springframework.beans.factory.UnsatisfiedDependencyException 中的异常:创建名称为“helloBean”的 bean 时出错:通过字段“地址”表示不满足的依赖关系:未定义 [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>
 -->

【问题讨论】:

  • 这应该可以帮助您解决问题:stackoverflow.com/a/26775786/1291150
  • 是的,我之前确实看到过这个链接 - 事实上在你提供的链接中 - 有两个答案 - 'Neelam Mehta' 和 'mcoolive' 准确地说明了我所做的事情,但它没有工作对我来说 - 接受的答案是'@Qualifier 用于通过名称或 id 引用 bean。由于它找不到名称或 id 为“small”的 xml 条目,因此它尝试按类型匹配,其中找到了两个 Size 实例。在我的例子中,我指的是 bean 的 id

标签: spring


【解决方案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>

【讨论】:

  • 谢谢 - 将阅读更多关于资源的信息。关于限定符 - 我认为这应该足够了 - @Qualifier("address1") 并且在应用程序上下文中我将它定义为 -
【解决方案2】:

你需要在配置xml中添加&lt;context:annotation-config /&gt;来解析@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>

【讨论】:

  • 是的,这行得通-但我认为 AutowiredAnnotationBeanPostProcessor 定义就足够了-显然不是-想知道为什么?
  • 需要激活通过 XML 注册的 bean 中的注释
【解决方案3】:

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 定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2016-12-30
    • 2016-12-05
    • 1970-01-01
    • 2017-05-04
    • 2014-08-14
    • 1970-01-01
    相关资源
    最近更新 更多