zoukankan      html  css  js  c++  java
  • 利用Spring的ApplicationEvent执行自定义方法

    在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<ContextRefreshedEvent> {
    
    	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配置文件:

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

  • 相关阅读:
    module5-01-jQuery 基础
    module4-JavaScript 高级特性、ES6 新特性
    module4-05-ES6新特性
    module4-04-正则表达式
    module4-03-继承和函数进阶
    module4-02-面向对象编程案例 随机方块、贪吃蛇
    module4-01-面向对象编程、原型链、构造函数、原型对象
    module3-Web APIs 网页应用编程
    module3-05-定时器的应用-简单动画-无缝滚动-轮播图
    人生赢家从规划开始,先觉知、量己力、衡外情、重实践、善反省
  • 原文地址:https://www.cnblogs.com/phil_jing/p/7141513.html
Copyright © 2011-2022 走看看