博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用Spring的ApplicationEvent执行自定义方法
阅读量:4336 次
发布时间:2019-06-07

本文共 1835 字,大约阅读时间需要 6 分钟。

在Spring中已经定义了五个标准事件,分别介绍如下:

1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件。
2)ContextClosedEvent:当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有单例Bean都被销毁
3) RequestHandleEvent:在Web应用中,当一个http请求(request)结束触发该事件
4)ContestStartedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Start()方法开始/重新开始容器时触发该事件
5)ContestStopedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Stop()方法停止容器时触发该事件
至于之后是否新增的暂时没研究

业务需求:项目启动时向数据库或者Redis初始化微信的access_token

新建SysInition(自定义名字)类,实现ApplicationListener接口,并监控ContextRefreshedEvent事件,在onApplicationEvent()方法中添加事件处理代码

import javax.annotation.Resource;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.stereotype.Component;import com.phil.hi.service.auth.AuthTokenService;/** * 项目初始化操作类 *  * @author phil * @date 2017年7月9日 * */public class SysInition implements ApplicationListener
{ private static final Logger logger = Logger.getLogger(SysInition.class); @Resource private AuthTokenService authTokenService; private static boolean flag = false; //防止二次调用 @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (!flag) { flag= true; String token = authTokenService.findToken(); if (StringUtils.isBlank(token)) { logger.info("token is null in db"); authTokenService.saveToken(); } } }}

spring配置文件:

注解定义

在类前加上@Component或@Controller,然后在<context:component-scan base-package="">里加上你的类所在的包名

解决onApplicationEvent(方法被执行两次以上的问题

原因:无论是使用spring还是spring mvc,系统会存在两个容器,一个是root application context ,另一个就是projectName-servletContext(作为root application context的子容器)。这种情况下,就会造成onApplicationEvent方法被执行两次。

解决方法:如代码所示,用个标志标记是否已执行

转载于:https://www.cnblogs.com/phil_jing/p/7141513.html

你可能感兴趣的文章
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------&gt;Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>