获取spring当前容器

1.实现ApplicationContextAware
public class SpringContext implements ApplicationContextAware{
	
	private ApplicationContext ac;
	
	private static SpringContext instance = null;
	
	public static SpringContext getInstance(){
		return instance;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.ac = applicationContext;
		instance = this;
	}
	
	/**
	 * @return the ac
	 */
	public ApplicationContext getAc() {
		return ac;
	}	
}


2.使用:没有声明deviceService属性,却在方法中想用容器管理的deviceService
IDeviceService deviceService =(IDeviceService) SpringContext.getInstance().getAc().getBean("deviceService");

猜你喜欢

转载自sha1064616837.iteye.com/blog/2203289