SpringContextHolder使用报错
版权声明:本文为博主原创文章,未经博主允许不得转载。 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使用报错的更多相关文章
- adb驱动安装和使用报错笔记
adb驱动安装 adb驱动下载地址:https://adb.clockworkmod.com/ 安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装 ...
- animate is not a function(zepto 使用报错)[转]
animate is not a function(zepto 使用报错) 1.为什么使用zepto写animate报错? 因为zepto默认构建包含: Core, Ajax, Event, Form ...
- 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 ...
- yum源使用报错
CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge. 服 ...
- 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa
docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...
- .net core中Grpc使用报错:The remote certificate is invalid according to the validation procedure.
因为Grpc采用HTTP/2作为通信协议,默认采用LTS/SSL加密方式传输,比如使用.net core启动一个服务端(被调用方)时: public static IHostBuilder Creat ...
- VirtualBox使用报错
VirtualBox使用报错 1.启动报错:Failed to instantiate CLSID_VirtualBox... 报错内容: Failed to instantiate CLSID_Vi ...
- antd-mobile使用报错
在第一次使用时,按照官网的进行配置,完了报错找不到antd-mobile下面的css 解决方法来源于 :https://github.com/ant-design/ant-design-mobile/ ...
- selenium使用报错“selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.”
安装了python3,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...
随机推荐
- Spring Boot 学习杂记
使用IntelliJ IDEA构建Spring Initializr
- aria2的安装与配置
aria2安装 安装 epel 源: yum install epel-release 然后直接安装: yum install aria2 -y 配置 Aria2 创建目录与配置文件 这一步需要切换到 ...
- 2019牛客多校第一场E ABBA(DP)题解
链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...
- luoguP3723 HNOI2017 礼物
链接 首先,两个手环增加非负整数亮度,等于其中一个增加一个整数亮度,可以为负. 令增加量为\(x\),旋转以后的原数列为,那么在不考虑转圈圈的情况下,现在的费用就是: \[\sum_{i=1}^n\l ...
- spring security基本知识(一)
spring security基本知识(一) Spring Security是为基于Spring的应用程序提供声明式安全保护的安全 性框架.Spring Security提供了完整的安全性解决方案,它 ...
- 在vue-cli中使用px2rem,配合lib-flexible使用
原文地址 附上github源码 看这里 1.下载lib-flexible npm安装 npm i lib-flexible --save 2.引入lib-flexible 在main.js中引入li ...
- HDU-2444-The Accomodation of Students(二分图判定,最大匹配)
链接: https://vjudge.net/problem/HDU-2444#author=634579757 题意: There are a group of students. Some of ...
- JVM调优总结(三)——分代垃圾回收详述
为什么要分代 分代的垃圾回收策略,是基于这样一个事实:不同的对象的生命周期是不一样的.因此,不同生命周期的对象可以采取不同的收集方式,以便提高回收效率. 在Java程序运行的过程中,会产生大量的对象, ...
- 从1G到5G发展史(3GPP是个什么组织 为啥5G标准离不开它)
1.“3GPP”组织建立的来龙去脉 3GPP一直以来在人们心中是一个神秘的组织,很多用户对于它的理解和认知,说不清,道不明.最近关于5G网络的诸多报道,都陈述了“5G网络”的标准是由“3GPP”来规定 ...
- net core 接受post值
public static string GetPostParams(HttpContext context) { string param = string.Empty; if (context.R ...