springboot-监听器

监听器

  • ApplicationListener可以实现这个接口时传入对应的监听器,用于监听该事件
  • 比如:实现 ApplicationListener<ContextRefreshedEvent> 接口,重写 onApplicationEvent 方法,将 ContextRefreshedEvent 对象传进去。如果我们想在加载或刷新应用上下文时,也重新刷新下我们预加载的资源,就可以通过监听 ContextRefreshedEvent 来做这样的事情。
package cn.tjhis.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * 描述 : 在这里配置 \META-INF\spring.factories
 * 1 org.springframework.boot.context.event.ApplicationStartingEvent
 * 2 org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
 * 3 org.springframework.boot.context.event.ApplicationContextInitializedEvent
 * 4 org.springframework.boot.context.event.ApplicationPreparedEvent
 * 5 org.springframework.context.event.ContextRefreshedEvent
 * 6 org.springframework.boot.context.event.ApplicationStartedEvent
 * 7 org.springframework.boot.availability.AvailabilityChangeEvent
 * 8 org.springframework.boot.context.event.ApplicationReadyEvent
 * 9 org.springframework.boot.availability.AvailabilityChangeEvent
 *
 * <p>路径 : cn.tjhis.listener
 *
 * <p>工程 : autobean
 *
 * <p>作者 : wanghx
 *
 * <p>日期 : 2023-04-02 11:17
 *
 * @author : Administrator
 */
public class MyListener implements ApplicationListener {
    private int count=0;
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        count++;
        System.out.printf("监听到事件: %5d %-1s",count,event.getClass().getName());
        System.out.println("");
    }
}

posted @   his365  阅读(259)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示