SpringContextHolder工具类获取spring对象
- 通过springContextHolder静态注入实体Bean
1.工具类用途?
该工具类主要用于那些没有归入spring框架管理的类却要调用spring容器中的bean提供的工具类,在spring中要通过IOC依赖注入来取得对应的对象,但是该类通过实现ApplicationContextAware接口,以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.如此就不能说说org.springframework.context.ApplicationContextAware这个接口了
2.ApplicationContextAware接口作用?
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。除了以上SpringContextHolder类之外,还有不需要多次加载spring配置文件就可以取得bean的类。
3.setApplicationContextAware( )何时执行?
Spring容器会检测容器中的所有Bean,如果发现某个Bean实现了ApplicationContextAware接口,Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContextAware()方法,调用该方法时,会将容器本身作为参数传给该方法——该方法中的实现部分将Spring传入的参数(容器本身)赋给该类对象的applicationContext实例变量,因此接下来可以通过该applicationContext实例变量来访问容器本身。
工具类
通过此工具类解决在静态方法中需要使用到Bean实例,而Autowired注入的实例不支持在静态方法中使用的问题.~ 可以根据name或.class对象获取Bean实
/**
* Copyright © All rights reserved.
*/
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
*/
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext = null;
private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
}
/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
if (logger.isDebugEnabled()) {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
}
applicationContext = null;
}
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
}
/**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
}
/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}
通过springContextHolder静态注入实体Bean
项目中的使用,例如
privateUserServiceuserService=
SpringContextHolder.getBean(UserService.class);
注意:
- 1.如果启动项目后报错 “applicaitonContext属性为null,请检查是否注入了SpringContextHolder!”,因为SpringContextHolder中的applicationContext为空,猜测是SpringContextHolder这个bean没有在UserRole这个bean加载前进行加载,导致没有加载完成,所以我们需要在配置文件中首先加载SpringContextHolder。把放在配置文件的第一个加载位置,再启动项目发现正常。
- 2.在使用该类静态方法时必须保证spring加载顺序正确, 也可以通过在使用类上添加 @DependsOn(“springContextHolder”),确保在此之前 SpringContextHolder 类已加载!