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

数组及Map的自动注入:

1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

(以上摘自https://blog.csdn.net/thinkingcao/article/details/71171222

List方式:

package com.imooc.beanannotation.multibean;

public interface BeanInterface {

}
package com.imooc.beanannotation.multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanImplOne implements BeanInterface {

}
package com.imooc.beanannotation.multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanImplTwo implements BeanInterface {

}
package com.imooc.beanannotation.multibean;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BeanInvoker {

	@Autowired
	private List<BeanInterface> list;
	
	public void say(){
		if(null!=list){
			for(BeanInterface bean:list){
				System.out.println(bean.getClass().getName());//getClass().getName()是用来返回Class对象所代表的具体对象的名称。
			}
		}else{
			System.out.println("List<BeanInterface> list is null");
		}
	}
	
	
}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.ioc.injection.service.InjectionService;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase{
	
	public TestBeanAnnotation(){
		super("classpath*:spring-beanannotation.xml");
	}
	
	@Test
	public void testAutowired(){
		InjectionService service=super.getBean("injectionServiceImpl");
		service.save("This is autowired.");
	}
}

	
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.multibean.BeanInvoker;
import com.imooc.ioc.injection.service.InjectionService;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
	
	public TestInjection(){
		super("classpath*:spring-beanannotation.xml");
	}

	@Test
	public void testAutowired(){
		InjectionService service=super.getBean("injectionServiceImpl");
		service.save("This is autowired");
	}
	
	@Test
	public void testMutiBean(){
		BeanInvoker invoker=super.getBean("beanInvoker");
		invoker.say();
	}
}

运行结果:

说明:Autowired注解能够完成把BeanInterface的两个实现类注解到list当中去

Map方式:

更改:

package com.imooc.beanannotation.multibean;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BeanInvoker {

	@Autowired
	private List<BeanInterface> list;
	
	@Autowired
	private Map<String,BeanInterface> map;
	
	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");
		}
	}
	
	
}

运行结果:

使用注解来实现:

package com.imooc.beanannotation.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(2)
@Component
public class BeanImplOne implements BeanInterface {

}
package com.imooc.beanannotation.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Component
public class BeanImplTwo implements BeanInterface {

}

运行结果:

说明:Order方法只针对于数组也就是list类型有效,对map无效(根据key来取值)。

区别:List是把bean的对象放进来;Map是把bean的id及bean的name和bean的实例一起放进来。

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