观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-3

@Qualifier

@Qualifier可缩小注解范围,也可指定唯一的bean。

package com.imooc.beanannotation.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;

@Component
public class BeanInvoker {

	@Autowired
	private List<BeanInterface> list;
	
	@Autowired
	private Map<String,BeanInterface> map;
	
	@Autowired
	@Qualifier("beanImplOne")
	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> is null");
		}
		
		System.out.println();
		if(null!=beanInterface){
			System.out.println(beanInterface.getClass().getName());//getClass().getName()是用来返回Class对象所代表的具体对象的名称。
		}else{
			System.out.println("beanInterface is null");
		}
	}
	
	
}

运行结果:

原文链接:加载失败,请重新获取