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

  • 相关阅读:
    Visual Basic 9.0 前沿播报·静态篇(二)对象初始化器和匿名类型
    Visual Basic 9.0 前沿播报内容概览
    Refactor! for VB —— VB2005的重构支持(一)
    我不知道该说什么,这里太先进了!
    Visual Basic 9.0 前沿播报·静态篇(一)局部变量类型推测和数组初始化器
    关于“就地颠倒句子里的词”面试题
    Visual Basic 9.0 前沿播报·静态篇(三)扩展方法
    《窝窝世界》视频20101025
    Silverlight 游戏开发小技巧:透明背景的Silverlight程序
    Silverlight 游戏开发小技巧:技能冷却效果2(Cooldown)2
  • 原文地址:https://www.cnblogs.com/phil_jing/p/7141513.html
Copyright © 2011-2022 走看看