Spring学习(9)--- @Autowired注解(二)

  • 可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,ApplicationEventPublisher,MessageSource
package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

public class MovieRecommender {

	@Autowired
	private ApplicationContext context;
	
	public MovieRecommender(){
	}
	
	//...
}
  •  可以通过添加注解给需要该类型的数组的字段和方法,以提供ApplicationContext中的所有特定类型的bean
private Set<MovieCatalog> movieCatalogs;

@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
	this.movieCatalogs = movieCatalogs;
}
  • 可以用于装配key为String的Map
private Map<String , MovieCatalog>  movieCatalog;

@Autowired
public void setMovieCatalog(Map<String, MovieCatalog> movieCatalog) {
	this.movieCatalog = movieCatalog;
}
  • 如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或使用的@Order注解
  • @Autowired是由Spring BeanPostProcessor处理的,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor类型应用这些注解,这些类型必须通过XML或者Spring的@Bean注解加载

数组及Map的自动注入——例子:

先定义一个BeanInterface接口

package com.multibean;

public interface BeanInterface {

}

在定义两个实现类

package com.multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanInterfaceImpl implements BeanInterface {

}
package com.multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanInterface2Impl implements BeanInterface{

	
}

定义BeanInvoker实现数组和Map

package com.multibean;

import java.util.*;

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){
			System.out.println("list...");
			for(BeanInterface bean:list){
				System.out.println(bean.getClass().getName());
			}
		}else{
			System.out.println("List<BeanInterface> list is null.");
		}
		
		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.");
		}
	}
	
}

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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">
         
        <context:component-scan base-package="com.multibean">
        </context:component-scan>   
</beans>

单元测试:

package com.multibean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UnitTest {

	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");
		BeanInvoker beanInvoker = (BeanInvoker)context.getBean("beanInvoker");
		beanInvoker.say();
	}
}

结果:

七月 06, 2015 10:46:41 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32482417: startup date [Mon Jul 06 22:46:41 CST 2015]; root of context hierarchy
七月 06, 2015 10:46:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
list...
com.multibean.BeanInterface2Impl
com.multibean.BeanInterfaceImpl
map...
beanInterface2Impl-----com.multibean.BeanInterface2Impl
beanInterfaceImpl-----com.multibean.BeanInterfaceImpl

@Order注解----例子:

改造一下两个实现类,加上@Order注解

package com.multibean;

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

@Order(value = 1)
@Component
public class BeanInterfaceImpl implements BeanInterface {

}
package com.multibean;

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

@Order(value = 2)
@Component
public class BeanInterface2Impl implements BeanInterface{

	
}

测试同上

结果:

七月 06, 2015 10:58:58 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e397bcb: startup date [Mon Jul 06 22:58:58 CST 2015]; root of context hierarchy
七月 06, 2015 10:58:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
list...
com.multibean.BeanInterfaceImpl
com.multibean.BeanInterface2Impl
map...
beanInterface2Impl-----com.multibean.BeanInterface2Impl
beanInterfaceImpl-----com.multibean.BeanInterfaceImpl

 PS:@Order只针对数组,对于map无效

原文地址:https://www.cnblogs.com/JsonShare/p/4625667.html