版权声明:本文为博主原创文章,未经博主允许不得转载。 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. [转载]【转】乘法器的Verilog HDL实现

      乘法器如果直接用*来实现的话,会消耗很多的资源.所以有了串行和并行两种实现思路.用串行的话,8位一般会有8位以上的延迟,但是消耗的资源是最少的.低速数据处理比较适合.并行也就是流水线方法,以时间换 ...

  2. html 常用小技巧

    style = "cursor:pointer;" 变小手 a{ text-decoration:none; } 或者把这个属性分别加到a标签下, a:link{ text-dec ...

  3. mysql个人初配置

    记录下在ubuntu 18.04.3 LTS环境中安装mysql和个人初期简单设置. 1:安装myslq:sudo apt install mysql-server. 2:设置root密码.由于我没有 ...

  4. PAT Basic 1081 检查密码 (15 分)

    本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母也有数字. 输入格式: 输入第一行 ...

  5. C++ STL(一)string的用法

    # 1.string的用法- string在<iostream>的头中就有,但是还是得用<string>的头 ##### 构造函数- str(const char* ch) 直 ...

  6. Codeforces 967 贪心服务器分配资源 线性基XOR递增序列构造

    A #include<bits/stdc++.h> using namespace std; ][] = {{, -}, {, }, { -, }, {, }}; typedef long ...

  7. Gym-100814K 数位DP 模拟除法

    Johnny is a brilliant mathematics student. He loves mathematics since he was a child, now he is work ...

  8. 如何使用Beyond Compare比较两个文件夹的差异

    很多时候,我们需要比较两个文件或者两个文件夹的差异性,看看是哪里不同.这时候就需要一款比较软件来处理,Beyond Compare就是其中一款非常好用的版本比较工具,下面简单介绍一下Beyond Co ...

  9. python爬虫及结巴分词《攀登者》影评分析

    <攀登者>影评爬取及分析 0.项目结构 其中simkai.ttf为字体文件,Windows查看系统自带的字体 C:\Windows\Fonts 一.爬取豆瓣影评数据 # -*- codin ...

  10. thinkphp之cookie操作

    cookie设置 命名空间 代码