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("");
}
}
世界上没有什么事情是跑步解决不了的,如果有,那就再跑一会!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!