`
sha1064616837
  • 浏览: 40461 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

获取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");
分享到:
评论
Global site tag (gtag.js) - Google Analytics