利用Spring的ApplicationEvent执行自定义方法
来源:互联网 发布:php apache 404 编辑:程序博客网 时间:2024/05/22 06:27
在Spring中已经定义了五个标准事件,分别介绍如下:
1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件。
2)ContextClosedEvent:当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有单例Bean都被销毁
3) RequestHandleEvent:在Web应用中,当一个http请求(request)结束触发该事件
4)ContestStartedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Start()方法开始/重新开始容器时触发该事件
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日 * */@Componentpublic class SysInition implements ApplicationListener<ContextRefreshedEvent> {private static final Logger logger = Logger.getLogger(SysInition.class);@Resourceprivate AuthTokenService authTokenService;private static boolean flag = false; //防止二次调用@Overridepublic 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();}}}}
配置文件:
<bean id="sysInition" class="com.phil.hi.config.SysInition"></bean>
注解定义
在类前加上@Component或@Controller,然后在<context:component-scan base-package="">里加上你的类所在的包名
解决onApplicationEvent(方法被执行两次以上的问题
原因:无论是使用spring还是spring mvc,系统会存在两个容器,一个是root application context ,另一个就是projectName-servletContext(作为root application context的子容器)。 这种情况下,就会造成onApplicationEvent方法被执行两次。
解决方法:如代码所示,用个标志标记是否已执行
阅读全文
0 0
- 利用Spring的ApplicationEvent执行自定义方法
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring的ApplicationEvent的使用
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent ,ApplicationListener,ApplicationContextAware
- spring 中 applicationEvent的使用
- Spring 的 ApplicationEvent and ApplicationListener
- FreeMarker 入门示例
- golang使用http client发起get和post请求示例
- 编程疯子
- Nginx映射项目外静态资源的实例
- JS输出斜边递增正三角
- 利用Spring的ApplicationEvent执行自定义方法
- Android静态注册的屏幕开启和屏幕关闭的广播捕捉不到的原因
- iOS开发笔记之六十六——基于Json的页面动态化方案
- mybatis之级联
- 使用JS实现的顺序表的基本操作
- div和span;选择器
- ROS机器人操作系统的安装、配置与初级教程 12
- struts2用注解开发出错一 There is no Action mapped for namespace [/] and action name [zhujie] associated with
- PostMessage