SpringBoot事件监听器
SpringBoot 事件监听
1)监听器
生命周期监听
-
监听器-SpringApplicationRunListener
-
自定义SpringApplicationRunListener来监听事件;
1.1. 编写SpringApplicationRunListener 实现类
1.2. 在 META-INF/spring.factories 中配置 org.springframework.boot.SpringApplicationRunListener=自己的Listener,还可以指定一个有参构造器,接受两个参数(SpringApplication application, String[] args)
/**
* Listener先要从 META-INF/spring.factories 读到
*
* 1、引导: 利用 BootstrapContext 引导整个项目启动
* starting: 应用开始,SpringApplication的run方法一调用,只要有了 BootstrapContext 就执行
* environmentPrepared: 环境准备好(把启动参数等绑定到环境变量中),但是ioc还没有创建;【调一次】
* 2、启动:
* contextPrepared: ioc容器创建并准备好,但是sources(主配置类)没加载。并关闭引导上下文;组件都没创建 【调一次】
* contextLoaded: ioc容器加载。主配置类加载进去了。但是ioc容器还没刷新(我们的bean没创建)。
* =======截止以前,ioc容器里面还没造bean呢=======
* started: ioc容器刷新了(所有bean造好了),但是 runner 没调用。
* ready: ioc容器刷新了(所有bean造好了),所有 runner 调用完了。
* 3、运行
* 以前步骤都正确执行,代表容器running。
*/
2)事件触发
SpringBoot-回调监听器
-
BootstrapRegistryInitializer
: 感知特定阶段:感知引导初始化 -
META-INF/spring.factories
- 创建引导上下文
bootstrapContext
的时候触发。 - application.
addBootstrapRegistryInitializer
(); - 场景:
进行密钥校对授权。
-
ApplicationContextInitializer: 感知特定阶段: 感知ioc容器初始化
-
META-INF/spring.factories
- application.addInitializers();
-
ApplicationListener: 感知全阶段:基于事件机制,感知事件。 一旦到了哪个阶段可以做别的事
-
@Bean
或@EventListener
:事件驱动
SpringApplication.addListeners(…)
或SpringApplicationBuilder.listeners(…)
META-INF/spring.factories
-
SpringApplicationRunListener: 感知全阶段生命周期 + 各种阶段都能自定义操作; 功能更完善。
-
META-INF/spring.factories
-
ApplicationRunner: 感知特定阶段:感知应用就绪Ready。卡死应用,就不会就绪
-
@Bean
-
CommandLineRunner: 感知特定阶段:感知应用就绪Ready。卡死应用,就不会就绪
-
@Bean
最佳实战:
- 如果项目启动前做事:
BootstrapRegistryInitializer
和ApplicationContextInitializer
- 如果想要在项目启动完成后做事:
**ApplicationRunner**
和**CommandLineRunner**
- 如果要干涉生命周期做事:
**SpringApplicationRunListener**
- 如果想要用事件机制:
**ApplicationListener**
实现CommandLineRunner:
#通过实现CommandLineRunner,在项目启动后进行一些初始化操作
@Component
@Slf4j
public class ClientStarterRunner implements CommandLineRunner {
/**
* @description 系统初始化实现
*/
@Autowired
private SystemInitService systemInitService;
/**
* @description 边缘初始化
*/
@Autowired
private EdgeInitService edgeInitService;
/**
* @description 健康检查初始化
*/
@Autowired
private HeartbeatTask heartbeatTask;
@Override
public void run(String... args) throws Exception {
log.info("--------------------- The First Step Init SDKService !!! ----------------");
systemInitService.init();
log.info("--------------------- The Second Step Init Camera Devices !!! ----------------");
edgeInitService.init();
log.info("--------------------- The Thread Step start Heartbeat Schedule Task !!! ----------------");
heartbeatTask.startHeartbeat();
}
}
SpringBoot-事件触发
``****9大事件触发顺序&时机`
ApplicationStartingEvent
:应用启动但未做任何事情, 除过注册listeners and initializers.ApplicationEnvironmentPreparedEvent
: Environment 准备好,但context 未创建.ApplicationContextInitializedEvent
: ApplicationContext 准备好,ApplicationContextInitializers 调用,但是任何bean未加载ApplicationPreparedEvent
: 容器刷新之前,bean定义信息加载ApplicationStartedEvent
: 容器刷新完成, runner未调用
=以下就开始插入了探针机制====
AvailabilityChangeEvent
:LivenessState.CORRECT
应用存活; 存活探针ApplicationReadyEvent
: 任何runner被调用AvailabilityChangeEvent
:ReadinessState.ACCEPTING_TRAFFIC
就绪探针,可以接请求ApplicationFailedEvent
:启动出错
)
3)SpringBoot 事件驱动开发
应用启动过程生命周期事件感知(9大事件)、应用运行中事件感知(无数种)。
-
事件发布:
ApplicationEventPublisherAware
或注入:ApplicationEventMulticaster
-
事件监听:
组件 + @EventListener
|事件发布者
@Service
public class EventPublisher implements ApplicationEventPublisherAware {
/**
* 底层发送事件用的组件,SpringBoot会通过ApplicationEventPublisherAware接口自动注入给我们
* 事件是广播出去的。所有监听这个事件的监听器都可以收到
*/
ApplicationEventPublisher applicationEventPublisher;
/**
* 所有事件都可以发
* @param event
*/
public void sendEvent(ApplicationEvent event) {
//调用底层API发送事件
applicationEventPublisher.publishEvent(event);
}
/**
* 会被自动调用,把真正发事件的底层组组件给我们注入进来
* @param applicationEventPublisher event publisher to be used by this object
*/
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
|事件订阅者
@Service
public class CouponService {
@Order(1)
@EventListener
public void onEvent(LoginSuccessEvent loginSuccessEvent){
System.out.println("===== CouponService ====感知到事件"+loginSuccessEvent);
UserEntity source = (UserEntity) loginSuccessEvent.getSource();
sendCoupon(source.getUsername());
}
public void sendCoupon(String username){
System.out.println(username + " 随机得到了一张优惠券");
}
}
|自定义心跳事件(发布订阅)
/*** ******************************** 一、定义心跳事件,继承ApplicationEvent,自定义事件监听*******************************************/
@Getter
@Setter
public class HeartbeatEvent extends ApplicationEvent {
/**
* @description Java Attribute variables
*/
private String message;
/**
* @description Java Attribute variables
*/
private Integer status;
/**
* @description Java Attribute variables
*/
private ScheduledExecutorService scheduler;
/**
* @param source 需要构造父类的source
* @param message
* @param status
* @param scheduler
* @return
*/
public HeartbeatEvent(Object source, String message, Integer status, ScheduledExecutorService scheduler) {
super(source);
this.message = message;
this.status = status;
this.scheduler = scheduler;
}
}
/*** *********************************** 二、定义心跳监听器,监听心跳事件*************************************************************/
@Slf4j
@Component
@RequiredArgsConstructor
public class HeartbeatEventListener {
/**
* 监听心跳返回的信息
*
* @param event
*/
@EventListener
public void onApplicationEvent(HeartbeatEvent event) {
log.info("onApplicationEvent received: {}", event.getMessage());
// 1.token过期
if (event.getStatus() == HeartbeatStatus.TOKEN_EXPIRATION.status) {
log.error("Token expired, stopping the heartbeat task and reloading register........");
}
// 2.配置变更,重新加载
if (event.getStatus() == HeartbeatStatus.CONFIG_CHANGE.status) {
log.info("The event was accepted successfully, the configuration changed");
}
}
}
/*** ***************************************** 三、构造事件,进行发送*************************************************************/
@Service
@Slf4j
@RequiredArgsConstructor
public class HeartbeatTask {
/**
* @description 注入ApplicationEventPublisher,进行事件发布
*/
private final ApplicationEventPublisher publisher;
/**
* @param message
* @param status
* @param scheduler
*/
private void pushHeartEvent(String message, Integer status, ScheduledExecutorService scheduler) {
HeartbeatEvent event = new HeartbeatEvent(this, message, status, scheduler);
publisher.publishEvent(event);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了