首页 / 博客文章 / 使用 SpringContextHolder获取bean实例

Java

使用 SpringContextHolder获取bean实例

Java实例  云邦 2021-12-03 00:56:48 1483 0条

SpringContextHolder 该类通过实现ApplicationContextAware接口,以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext

主要用于那些没有归入spring框架管理的类却要调用spring容器中的bean提供的工具类,在spring中要通过IOC依赖注入来取得对应的对象,

ApplicationContextAware接口作用
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。除了以上SpringContextHolder类之外,还有不需要多次加载spring配置文件就可以取得bean的类。

1、SpringContextHolder 具体实现:

  1. import java.util.Map;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. /**
  5. *
  6. *以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
  7. * @author Bucky
  8. */
  9. @Service
  10. @Lazy(false)
  11. public class SpringContextHolder implements ApplicationContextAware{
  12. private static ApplicationContext applicationContext;
  13. //实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
  14. public void setApplicationContext(ApplicationContext applicationContext) {
  15. SpringContextHolder.applicationContext = applicationContext;
  16. }
  17. //取得存储在静态变量中的ApplicationContext.
  18. public static ApplicationContext getApplicationContext() {
  19. checkApplicationContext();
  20. return applicationContext;
  21. }
  22. //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
  23. @SuppressWarnings("unchecked")
  24. public static <T> T getBean(String name) {
  25. checkApplicationContext();
  26. return (T) applicationContext.getBean(name);
  27. }
  28. //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
  29. //如果有多个Bean符合Class, 取出第一个.
  30. @SuppressWarnings("unchecked")
  31. public static <T> T getBean(Class<T> clazz) {
  32. checkApplicationContext();
  33. @SuppressWarnings("rawtypes")
  34. Map beanMaps = applicationContext.getBeansOfType(clazz);
  35. if (beanMaps!=null && !beanMaps.isEmpty()) {
  36. return (T) beanMaps.values().iterator().next();
  37. } else{
  38. return null;
  39. }
  40. }
  41. private static void checkApplicationContext() {
  42. if (applicationContext == null) {
  43. throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
  44. }
  45. }
  46. }

2、修改配置文件spring-context.xml,添加bean:

  1. <bean id="springContextHolder" class="com.utils.SpringContextHolder" lazy-init="false"/>

3、项目使用:

  1. private UserService userService= SpringContextHolder.getBean(UserService.class);

注意要是在启动时出现 applicaitonContext属性未注入异常,则检查下是否注入了SpringContextHolder!”,因为SpringContextHolder中的applicationContext为空,可能是SpringContextHolder这个bean没有在UserRole这个bean加载前进行加载,导致没有加载完成,所以我们需要在配置文件中首先加载SpringContextHolder。把放在配置文件的第一个加载位置,再启动。

  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)

文章评论

置顶