/**
* 为不是spring所管理的对象,需要引用spring管理对象的时候所用的工具类
* @author he_guanhong
*
*/
public class SpringApplicationContext implements ApplicationContextAware{
protected static ApplicationContext context;
@Override
public void setApplicationContext(
org.springframework.context.ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
context = (ApplicationContext) applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
//MyBean myBean = (MyBean) SpringApplicationContext.getContext().getBean("myBean");
}
在其他类中使用:
TSAService tsaService = SpringApplicationContext.getContext().getBean(TSAService.class);
本文介绍了一个用于非Spring管理对象引用Spring管理对象的工具类——SpringApplicationContext。该工具类通过实现ApplicationContextAware接口,允许开发者在非Spring管理的环境中获取Spring容器中的Bean实例。文章提供了具体的实现代码及使用示例。

1383

被折叠的 条评论
为什么被折叠?



