spring中Qualifier注解(10)

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章