5、spring自动装配之@Qualifier注解的使用
2017-02-18 14:06
519 查看
在上一篇4、spring使用@Autowired注解实现自动装配 教程中,我们知道了怎么使用@Autowired注解进行自动装配。但是当存在两个类型一致的person bean时,将会有什么情况出现。我们一起来看看下面的例子:
第一步:创建bean
Customer类
Person类
第二步:修改beans.xml配置文件
说明:这里存在了两个person bean,一个是person1,一个是person2
第三步:编写测试代码
测试结果如下:(抛出异常)
这是因为Customer在进行自动装配时,不清楚要装载person1,还是要装载person2,这时我们就应该使用@Qualifier注解来告诉它,我们想要的是哪一个!!
为解决上述问题,这里引入@Qualifier注解
@Qualifier示例
修改你的Customer类如下:
修改你的beans.xml配置文件如下:
此时应该将引入spring上下文以及context:annotation-config标签
ba2c
此时Customer bean将会自动装配person2 bean
运行结果如下:
例子
说明:如果已经看了上一篇教程,可以直接跳到第二步第一步:创建bean
Customer类
package com.main.autowrite.autowired.annotation; import org.springframework.beans.factory.annotation.Autowired; public class Customer { //即将自动装配的属性 private Person person; private String type; @Autowired public Customer(Person person) { this.person = person; } //getter and setter methods //toString methods }
Person类
package com.main.autowrite.autowired.annotation; public class Person { private String name; private int age; //getter and setter methods //toString methods }
第二步:修改beans.xml配置文件
说明:这里存在了两个person bean,一个是person1,一个是person2
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person1" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean> <bean id="person2" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="tom" /> <property name="age" value="28"/> </bean> </beans>
第三步:编写测试代码
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/autowired/annotation/beans.xml"); Customer customer = (Customer)context.getBean("customer"); System.out.print(customer.toString()); }
测试结果如下:(抛出异常)
这是因为Customer在进行自动装配时,不清楚要装载person1,还是要装载person2,这时我们就应该使用@Qualifier注解来告诉它,我们想要的是哪一个!!
为解决上述问题,这里引入@Qualifier注解
@Qualifier示例
修改你的Customer类如下:
package com.main.autowrite.autowired.annotation; import java.lang.annotation.Retention; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Customer { //即将自动装配的属性 @Autowired @Qualifier("person2") private Person person; private String type; //getter and setter methods //toString methods }
修改你的beans.xml配置文件如下:
此时应该将引入spring上下文以及context:annotation-config标签
ba2c
<?xml version="1.0" encoding="UTF-8"?> <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-2.5.xsd"> <context:annotation-config /> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person1" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean> <bean id="person2" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="tom" /> <property name="age" value="28"/> </bean> </beans>
此时Customer bean将会自动装配person2 bean
运行结果如下:
相关文章推荐
- Spring整理系列(10)——@Autowired自动装配、结合@Qualifier过滤及与JSR-250@Resource注解、JSR-330@Inject注解的区别、@Named使用
- 4、spring使用@Autowired注解实现自动装配
- Spring使用@Autowired注解自动装配
- spring 使用注解自动装配和自动扫描机制
- Spring--使用注解和自动装配
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- spring-使用注解实现Bean自动装配2
- spring-使用注解实现Bean自动装配1
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- Spring 全注解配置 bean 和 调用 (5) @Qualifier 自动装配歧义性消除
- Spring学习记录(十一)---使用注解和自动装配
- Spring学习(六)-----Spring使用@Autowired注解自动装配
- Spring--使用注解和自动装配
- Spring 使用注解自动装配
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- Spring2.5中使用注解装配属性and组件自动扫描
- Spring自动注解装配、自动检测装配Bean配合过滤组件使用
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解
- 7.5.6: Spring容器中的Bean---使用自动装配注入合作者Bean