zoukankan      html  css  js  c++  java
  • ContextRefreshedEvent事件使用注意事项(Spring)

    0 概述
    ContextRefreshedEvent 事件会在Spring容器初始化完成会触发该事件。我们在实际工作也可以能会监听该事件去做一些事情,但是有时候使用不当也会带来一些问题。

    1 防止重复触发
    主要因为对于web应用会出现父子容器,这样就会触发两次,那么如何避免呢?下面给出一种简单的解决方案。

    @Component
    public class TestTask implements ApplicationListener<ContextRefreshedEvent> {
        private volatile AtomicBoolean isInit=new AtomicBoolean(false);
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            //防止重复触发
            if(!isInit.compareAndSet(false,true)) {
                return;
            }
            start();
        }
    
        private void start() {
            //开启任务
            System.out.println("****-------------------init---------------******");
        }
    }
    

      

    2 监听事件顺序问题

    Spring 提供了一个SmartApplicationListener类,可以支持listener之间的触发顺序,普通的ApplicationListener优先级最低(最后触发)。

    @Component
    public class LastTask implements SmartApplicationListener {
        private volatile AtomicBoolean isInit = new AtomicBoolean(false);
        @Override
        public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
            return eventType == ContextRefreshedEvent.class;
        }
    
        @Override
        public boolean supportsSourceType(Class<?> sourceType) {
            return true;
        }
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if (!isInit.compareAndSet(false, true)) {
                return;
            }
            start();
        }
    
        private void start() {
            //开启任务
            System.out.println("LastTask-------------------init------------ ");
        }
    
        //值越小,就先触发
        @Override
        public int getOrder() {
            return 2;
        }
    }
    

      

  • 相关阅读:
    splay
    开车旅行(2012day1T3)
    LCT入门
    最小瓶颈路
    poj 3041 Asteroids
    sql waitfor 延时执行
    [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
    SQL Server中行列转换
    sql中 with rollup 、with cube、grouping 统计函数用法
    sql 分组后 组内排名
  • 原文地址:https://www.cnblogs.com/yuyu666/p/10071770.html
Copyright © 2011-2022 走看看