关于Spring JavaWeb工程中的ContextRefreshedEvent事件
在应用启动时,通常想在此时预加载一些资源,全局使用。
Spring会在操作应用上下文时,使用ApplicationEventPublisher触发相关ApplicationContextEvent,我们可以监听这些事件来做一些事情。
Spring中ApplicationContextEvent有以下几种:

其中ContextRefreshedEvent的执行时机为:
Event raised when an {@code ApplicationContext} gets initialized or refreshed.
我们通常会在Spring加载或刷新应用上下文时,也重新刷新下我们预加载的资源,我们就可以通过监听ContextRefreshedEvent来做这样的事情。
代码如下:
@Component
public class SpringHandlersProvider implements ApplicationListener<ContextRefreshedEvent> {
Lists<XXX> handlerList = Lists.newHashList();
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//do something
handlerList.add(xxx);
}
}
但对于tomcat工程来说,我们一般会加载两个上下文容器一个父容器,一个mvc子容器
- 父容器{@code ContextRefreshedEvent[source=Root WebApplicationContext: startup date [Thu Sep 29 14:52:08 CST 2016]; root of context hierarchy]}
- mvc容器{@code ContextRefreshedEvent[source=WebApplicationContext for namespace 'springmvc-servlet': startup date [Thu Sep 29 14:52:34 CST 2016]; parent: Root WebApplicationContext]}
这样就会触发两次ContextRefreshedEvent事件,导致监听此事件所作的逻辑执行两次。
避免方法:
1:只在加载父容器时,执行一次
@Component
public class SpringHandlersProvider implements ApplicationListener<ContextRefreshedEvent> {
Lists<XXX> handlerList = Lists.newHashList();
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (Predicates.isNull().apply(event.getApplicationContext().getParent())) {
//do something
handlerList.add(xxx);
}
}
}
2:每次执行onApplicationEvent()方法时就将存放资源的容器清空下
@Component
public class SpringHandlersProvider implements ApplicationListener<ContextRefreshedEvent> {
Lists<XXX> handlerList = Lists.newHashList();
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
handlerList.clear(); //do something
handlerList.add(xxx);
}
}
关于Spring JavaWeb工程中的ContextRefreshedEvent事件的更多相关文章
- Spring透过ApplicationListener来触发contextrefreshedevent事件
Spring通过ApplicationListener接口来触发contextrefreshedevent事件在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据.加载 ...
- JavaWeb工程中web.xml基本配置
一.理论准备 先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对. web.xml是不是必须的呢?不是的,只要你不用到里面的配置信息就好了,不过在大型web工程下使用该文 ...
- JavaWeb工程中web.xml基本配置(转载学习)
一.理论准备 先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对. web.xml是不是必须的呢?不是的,只要你不用到里面的配置信息就好了,不过在大型web工程下使用该文 ...
- JavaWeb工程中url地址的写法
两种url地址: 1. "/"给服务器使用, 代表web工程根路径(webroot)2. "/"给浏览器使用, 代表tomcat 目录下的webapps文件夹 ...
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)转
关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) 应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用 ...
- Spring中ApplicationContext对事件的支持
Spring中ApplicationContext对事件的支持 ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) 应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用 ...
- SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
随机推荐
- 移动端fixed定位在底部,出现键盘后消失
jq var h=$(window).height(); $(window).resize(function() { if($(window).height()<h){ $('.nav').hi ...
- iOS block 用法
1.定义Block /* 回传void ,参数也是void 的block*/ void (^blockReturningVoidWithVoidArgument)( void ); /* 回传整数,两 ...
- Permutations 全排列 回溯
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- Jeff Dean 光辉事迹
这是Google 2007年的愚人节笑话,罗列了很多Jeff Dean的“光辉事迹”.大名鼎鼎的Jeff Dean想必不用我介绍了.……好吧,还是介绍一下,Jeff Dean是Google最早的一批员 ...
- FastAdmin 自学教程 - 目录(持续更新)(2019-10-11)
FastAdmin 自学教程 - 目录 本自学教程将不定期更新. 了解 FastAdmin FastAdmin 开发第 1 天:了解 FastAdmin 框架 FastAdmin 开发第 2 天:安装 ...
- 2019-9-2-dotnet-命名管道名字长度限制
title author date CreateTime categories dotnet 命名管道名字长度限制 lindexi 2019-09-02 11:54:50 +0800 2019-09- ...
- Linux使用及命令
#命令模式下输入:光标移动到第34行第15个字符 <Enter>15l(这是小写的L) ctrl+u删除光标前面的字符 ctrl+j删除光标后面的字符 在Linux下用VIM打开大小几个G ...
- Atcoder Tenka1 Programmer Contest D: IntegerotS 【思维题,位运算】
http://tenka1-2017.contest.atcoder.jp/tasks/tenka1_2017_d 给定N,K和A1...AN,B1...BN,选取若干个Ai使它们的或运算值小于等于K ...
- 2019-8-31-dotnet-core-输出调试信息到-DebugView-软件
title author date CreateTime categories dotnet core 输出调试信息到 DebugView 软件 lindexi 2019-08-31 16:55:58 ...
- 阿里云:面向5G时代的物联网无线连接服务
在4月24日落幕的2019中国联通合作伙伴大会“5G+物联网(IoT)论坛”上,阿里云高级运营专家李茁出席圆桌对话,分享了5G时代物联网如何更好地推动行业完成生产.管理和商业模式的创新,阿里云又会以何 ...