欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring获取applicationContext的最佳方式 博客分类: spring spring 

程序员文章站 2024-03-14 12:08:04
...
public class SpringContextUtil implements ApplicationContextAware{
     private static  ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.applicationContext = applicationContext;
	}
	public static <T> T getBean(String name){
		return  (T) applicationContext.getBean(name);
	}

}
//然后在applicationContext.xml中配置
<bean id="springContextUtil" class="xxxxxxx"></bean>
相关标签: spring