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