1. 介绍与使用
SpringContextHolder工具类的方法 getBean
可以获取 bean 的实例。但是工具类的方法是不是 static 的,等同于 @Autowired
注入。
解决问题:
类中方法是 static,方法中使用的对象也必须是 static,但正常情况下 @Autowired
无法注入静态的 bean,于是发现项目中用到了springContextHolder,通过使用;
UserService userService = SpringContextHolder.getBean(UserService.class); // 获取bean
2. 异常
使用 SpringContextHolder 来获取一个bean报异常:
Java.lang.IllegalStateException: applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.
at org.apache.commons.lang3.Validate.validState(Validate.java:826)
at com.jituan.common.util.SpringContextHolder.assertContextInjected(SpringContextHolder.java:79)
at com.jituan.common.util.SpringContextHolder.getBean(SpringContextHolder.java:41)
at com.jituan.common.message.util.sms.SmsUtil.querySmsTemplate(SmsUtil.java:206)
at com.jituan.common.message.util.sms.SmsUtil.send(SmsUtil.java:76)
at com.jituan.common.message.processer.SmsProcesser.send(SmsProcesser.java:37)
at com.jituan.batch.msghandler.MessageHandler.smsSend(MessageHandler.java:106)
at com.jituan.batch.msghandler.MessageHandler$SmsTread.run(MessageHandler.java:185)
配置 spring-mvc.xml
<!-- 设全局变量以便可以获得对应的注入bean -->
<bean id="springContextHolder" class="com.jituan.common.util.SpringContextHolder" />
编写 SpringContextHolder
代码:
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @description:spring context 上下文
* @author: zhangx
* @Date: 2018/1/30 16:40
*/
@Component
@Slf4j
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext = null;
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
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() {
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
applicationContext = null;
}
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
log.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext);
if (SpringContextHolder.applicationContext != null) {
log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
+ SpringContextHolder.applicationContext);
}
SpringContextHolder.applicationContext = applicationContext;
}
/**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
}
/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
if (applicationContext == null) {
throw new IllegalArgumentException(
"applicationContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}
}