Spring 弹簧@Qualifier注释不工作
我在玩Spring 弹簧@Qualifier注释不工作,spring,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/
@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>
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();
}
}
我一直在得到这个异常-理想情况下我不应该,因为我已经定义了id为'address1'的@Qualifier注释-所以它不应该抛出异常
警告:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatifiedpendencyException:创建名为“helloBean”的bean时出错:通过字段“address”表示的未满足的依赖项:没有类型为[com.springex1.address]的符合条件的bean已定义:预期为单个匹配bean,但找到2:address1,address2;嵌套异常为org.springframework.beans.factory.NoniqueBeandDefinitionException:未定义[com.springex1.Address]类型的合格bean:应为单个匹配bean,但找到2:address1,address2
线程“main”org.springframework.beans.factory.unsatifiedpendencyException中的异常:创建名为“helloBean”的bean时出错:通过字段“address”表示的未满足的依赖关系:未定义类型为[com.springex1.address]的合格bean:应为单个匹配bean,但找到2:address1,address2;嵌套异常为org.springframework.beans.factory.NoniqueBeandDefinitionException:未定义[com.springex1.Address]类型的合格bean:应为单个匹配bean,但找到2:address1,address2
位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.inject(AutoWiredNotationBeanPostProcessor.java:573)
我使用的是Spring4.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>
-->
org.springframework
spring上下文
4.3.0.1发布
您需要将
添加到配置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>
首先,Spring文档建议不要使用
@限定符
,而是建议使用@Resource
注释,该注释按名称进行注入(如限定符)
因此,选项一是将@限定符
替换为@Resource
注释
但是为了使IOC能够正确地使用@Resource
进行注入,您应该
姓名地址地址
至地址地址1
现在,如果仍要使用@限定符
,则必须将配置更改为:
<bean id="address1" class="com.springex1.Address" >
<property name="street" value="sahajanand" />
<qualifier value="address1"/>
</bean>
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 />
或:
通过添加其中一个标记,您可以从xml中删除AutowiredNotationBeanPostProcessor定义。这应该可以帮助您解决问题:是的,我以前确实看到过此链接-事实上,在您提供的链接中-有两个答案-由“Neelam Mehta”和“mcoolive”精确指定了我所做的工作,但它不起作用me-被接受的答案是“@限定符用于通过名称或id引用bean。因为它找不到名称或id为“small”的xml条目,所以它尝试按类型匹配,在我的例子中,我是通过它的田园诗来指这个bean的,这是有效的,但我认为AutowiredNotationBeanPostProcessor的定义已经足够了,显然还不够,我想知道为什么?需要激活通过XMLThank注册的bean中的注释。将阅读更多关于资源的信息。关于限定符-我认为这应该足够了-@Qualifier(“address1”),在应用程序上下文中,我将其定义为-
<context:annotation-config />
<context:component-scan base-package="com.test.package" />