版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dobecoder/article/details/88401612
今天在使用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类上加入注解@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");
}
}

---------------------
作者:90后佛系程序员
来源:CSDN
原文:https://blog.csdn.net/dobecoder/article/details/88401612
版权声明:本文为博主原创文章,转载请附上博文链接!

SpringContextHolder使用报错的更多相关文章

  1. adb驱动安装和使用报错笔记

    adb驱动安装 adb驱动下载地址:https://adb.clockworkmod.com/ 安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装 ...

  2. animate is not a function(zepto 使用报错)[转]

    animate is not a function(zepto 使用报错) 1.为什么使用zepto写animate报错? 因为zepto默认构建包含: Core, Ajax, Event, Form ...

  3. Windows下Git使用报错:warning:LF will be replaced by CRLF in ××××.××

    Windows下Git使用报错: warning:LF will be replaced by CRLF in ××××.××(文件名) The file will have its original ...

  4. yum源使用报错

    CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge. 服 ...

  5. 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa

    docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...

  6. .net core中Grpc使用报错:The remote certificate is invalid according to the validation procedure.

    因为Grpc采用HTTP/2作为通信协议,默认采用LTS/SSL加密方式传输,比如使用.net core启动一个服务端(被调用方)时: public static IHostBuilder Creat ...

  7. VirtualBox使用报错

    VirtualBox使用报错 1.启动报错:Failed to instantiate CLSID_VirtualBox... 报错内容: Failed to instantiate CLSID_Vi ...

  8. antd-mobile使用报错

    在第一次使用时,按照官网的进行配置,完了报错找不到antd-mobile下面的css 解决方法来源于 :https://github.com/ant-design/ant-design-mobile/ ...

  9. selenium使用报错“selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.”

    安装了python3,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...

随机推荐

  1. java.lang.String (JDK 1.8)

    研究一个类之前我们来说说类的构成:              一个类主要分为几个部分:分别是类的定义(类的继承,接口的实现),全局变量方法,属性,内部类等. 第一点: 实现的接口 public fin ...

  2. 一键部署lnmp脚本

    先下载好nginx安装包,解包之后可以执行下面的脚本,一键部署 cd nginx-1.12.2 useradd -s /sbin/nologin nginx./configuremakemake in ...

  3. 微服务中的rpc 请求写法

    1.采用restmplate 的postForObject RestTemplate template = new RestTemplate(); HttpEntity<List<Map& ...

  4. 常见状态码StatusCode

    当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求. ...

  5. C++为什么不可以把一个数组直接赋值给另一个数组

    今天好奇一个问题, int a[3] = {1,2,3]; int b[3]; b=a; 编译器报错, 网上找了一圈, 大概明白: C++就是如此设定的, 数组不能直接赋值, 可以使用std::cop ...

  6. postgresql查询栅格数据范围(extent)

    栅格数据: SELECT ST_Extent(rast::geometry) as bextent FROM tmean_19; 矢量数据: SELECT ST_Extent(way) as bext ...

  7. 对vue源码之缓存的研究--------------引用

    探索vue源码之缓存篇 一.从链表说起 首先我们来看一下链表的定义: 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个 ...

  8. Spring Boot 的各种start

    新建一个springBoot项目时,你会选择很多依赖,在项目中的build.gradle中你会看见各种start,例如下边的代码: 今天就在这里列举一下各种start: 1.spring-boot-s ...

  9. 文本框——EditText

    长度

  10. SpringCloud 入门知识篇

    SpringCloud 入门 springcloud 学习 7天学会springcloud 教程 https://www.cnblogs.com/skyblog/category/738524.htm ...