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");
分享到:
评论