利用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()方法开始/重新开始容器时触发该事件

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方法被执行两次。
解决方法:如代码所示,用个标志标记是否已执行

原创粉丝点击