1. 介绍与使用

SpringContextHolder工具类的方法 getBean 可以获取 bean 的实例。但是工具类的方法是不是 static 的,等同于 @Autowired 注入。

解决问题:

类中方法是 static,方法中使用的对象也必须是 static,但正常情况下 @Autowired 无法注入静态的 bean,于是发现项目中用到了springContextHolder,通过使用;

  1. UserService userService = SpringContextHolder.getBean(UserService.class); // 获取bean

2. 异常  

使用 SpringContextHolder 来获取一个bean报异常:

  1. Java.lang.IllegalStateException: applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.
  2. at org.apache.commons.lang3.Validate.validState(Validate.java:826)
  3. at com.jituan.common.util.SpringContextHolder.assertContextInjected(SpringContextHolder.java:79)
  4. at com.jituan.common.util.SpringContextHolder.getBean(SpringContextHolder.java:41)
  5. at com.jituan.common.message.util.sms.SmsUtil.querySmsTemplate(SmsUtil.java:206)
  6. at com.jituan.common.message.util.sms.SmsUtil.send(SmsUtil.java:76)
  7. at com.jituan.common.message.processer.SmsProcesser.send(SmsProcesser.java:37)
  8. at com.jituan.batch.msghandler.MessageHandler.smsSend(MessageHandler.java:106)
  9. at com.jituan.batch.msghandler.MessageHandler$SmsTread.run(MessageHandler.java:185)

配置 spring-mvc.xml

  1. <!-- 设全局变量以便可以获得对应的注入bean -->
  2. <bean id="springContextHolder" class="com.jituan.common.util.SpringContextHolder" />

编写 SpringContextHolder 代码:

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.beans.factory.DisposableBean;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * @description:spring context 上下文
  8. * @author: zhangx
  9. * @Date: 2018/1/30 16:40
  10. */
  11. @Component
  12. @Slf4j
  13. public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
  14. private static ApplicationContext applicationContext = null;
  15. /**
  16. * 取得存储在静态变量中的ApplicationContext.
  17. */
  18. public static ApplicationContext getApplicationContext() {
  19. assertContextInjected();
  20. return applicationContext;
  21. }
  22. /**
  23. * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
  24. */
  25. public static <T> T getBean(String name) {
  26. assertContextInjected();
  27. return (T) applicationContext.getBean(name);
  28. }
  29. /**
  30. * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
  31. */
  32. public static <T> T getBean(Class<T> requiredType) {
  33. assertContextInjected();
  34. return applicationContext.getBean(requiredType);
  35. }
  36. /**
  37. * 清除SpringContextHolder中的ApplicationContext为Null.
  38. */
  39. public static void clearHolder() {
  40. log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
  41. applicationContext = null;
  42. }
  43. /**
  44. * 实现ApplicationContextAware接口, 注入Context到静态变量中.
  45. */
  46. @Override
  47. public void setApplicationContext(ApplicationContext applicationContext) {
  48. log.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext);
  49. if (SpringContextHolder.applicationContext != null) {
  50. log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
  51. + SpringContextHolder.applicationContext);
  52. }
  53. SpringContextHolder.applicationContext = applicationContext;
  54. }
  55. /**
  56. * 实现DisposableBean接口, 在Context关闭时清理静态变量.
  57. */
  58. @Override
  59. public void destroy() throws Exception {
  60. SpringContextHolder.clearHolder();
  61. }
  62. /**
  63. * 检查ApplicationContext不为空.
  64. */
  65. private static void assertContextInjected() {
  66. if (applicationContext == null) {
  67. throw new IllegalArgumentException(
  68. "applicationContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
  69. }
  70. }
  71. }