SpringContextHolder使用报错

  • 今天在使用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)
  • 解决方案:
  1. 在spring-mvc.xml中加入配置后解决:
<!-- 全局变量以便可以获得对应的注入bean -->
    <bean id="springContextHolder" class="com.jituan.common.util.SpringContextHolder" />
  1. 在SpringContextHolder类上加入注解@Service、@Lazy(false)
  • SpringContextHolder 具体实现:
import java.util.Map;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
 *
 *以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
 * @author Bucky
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware{
 
    private static ApplicationContext applicationContext;
 
     
    //实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }
 
    
    //取得存储在静态变量中的ApplicationContext.
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }
     
    //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }
 
     
    //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    //如果有多个Bean符合Class, 取出第一个.
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        @SuppressWarnings("rawtypes")
                Map beanMaps = applicationContext.getBeansOfType(clazz);
        if (beanMaps!=null && !beanMaps.isEmpty()) {
            return (T) beanMaps.values().iterator().next();
        } else{
            return null;
        }
    }
 
    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
        }
    }
 
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,851评论 2 22
  • 1.1 Spring IoC容器和bean简介 本章介绍了Spring Framework实现的控制反转(IoC)...
    起名真是难阅读 2,672评论 0 8
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,554评论 19 139
  • 这或许是我继《寒战》之后看的第二部悬疑类电影了,同样很出彩。这部《记忆碎片》是由有“韩国黑泽明”之称的朴裕焕担任导...
    季霖阅读 28,064评论 5 4
  • 在这样一个多物质、快节奏的时代,让您减少外出就餐?谈生意、工作忙、要应酬、没时间、太麻烦等等一个个理由摆在...
    玥禾阅读 783评论 0 51

友情链接更多精彩内容