spring中Qualifier注解(10)
2016-01-16 16:59
393 查看
2016/1/16 16:32:06
1.@Qualifier
按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以用于指定单独的构造器参数或方法参数可以用于注解集合类型变量
如果通过名字进行注解注入,主要是使用的不是@Autowired(即使在技术上能够通过@Qualifier指定bean的名字),替代方式是使用JSR-250@Resource注解,它是通过其独特的名称来识别特定的目标(这是一个与所声明的类型是无关的匹配过程)
因语义差异,集合或Map类型的bean无法通过@Autowired来注入因为没有类型匹配到这样的bean,为这些bean使用@Resource注解,通过唯一名称引用集合或Map的bean
2.使用范围的区别
@AutoWIred适用于fields,constructors,multi-argument methods这些允许在参数级别使用@Qualifier注解缩小范围的情况@Resource适用于成员变量,只有一个参数的setter方法,所以在目标是构造器或一个多参数方法时,最好的方式是使用qualifiers
3.例子
XML:spring-autowired.xml<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.zjx"></context:component-scan> </beans>
在BeanInvoker类中增加一个成员变量,因为我们不知道BeanInterface是实现的哪一个类,通过@Qualifier注解缩小范围为id为”beanTwoImpl”的这个实现类
其余类参见spring中AutoWired(9)中的介绍
package com.zjx.multibean; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; /** * @author acer 定义一个包装类 * */ @Component public class BeanInvoker { @Autowired private List<BeanInterface> list; @Autowired private Map<String, BeanInterface> map; @Autowired @Qualifier("beanTwoImpl") private BeanInterface beanInterface; public void say() { if (null != list && 0 != list.size()) { System.out.println("list..."); for (BeanInterface bean : list) { System.out.println(bean.getClass().getName()); } } else { System.out.println("List<BeanInterface> list is null!"); } System.out.println(); if (null != map && 0 != map.size()) { System.out.println("map..."); for (Map.Entry<String, BeanInterface> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue().getClass().getName()); } } else { System.out.println("Map<String,BeanInterface> map is null!"); } System.out.println(); if(null!=beanInterface){ System.out.println(beanInterface.getClass().getName()); }else{ System.out.println("beanInterface is null!"); } } }
测试:
package com.zjx.interfaces.test;
import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.zjx.multibean.BeanInvoker; @RunWith(BlockJUnit4ClassRunner.class) public class TestMultiBean extends UnitTestBase{ public TestMultiBean() { super("classpath:spring-autowired.xml"); } @Test public void testMultiBean(){ BeanInvoker invoker = super.getBean("beanInvoker"); invoker.say(); } }
结果:
list... com.zjx.multibean.BeanTwoImpl com.zjx.multibean.BeanOneImpl map... beanOneImpl com.zjx.multibean.BeanOneImpl beanTwoImpl com.zjx.multibean.BeanTwoImpl com.zjx.multibean.BeanTwoImpl
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 交换机升级排障实例
- sql2008启动代理未将对象应用到实例解决方案
- 初识JQuery 实例一(first)
- JQuery入门基础小实例(1)
- Jquery具体实例介绍AJAX何时用,AJAX应该在什么地方用
- 一个小助手批处理实例代码
- 模拟Spring的简单实现
- XStream使用方法总结附实例代码
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- asp.net得到本机数据库实例的两种方法代码
- C++多继承同名隐藏实例详细介绍
- PHP入门学习的几个不错的实例代码
- JSP发送邮件实例
- spring的jdbctemplate的crud的基类dao