Spring自定义事件
ApplicationContext 提供了一套事件机制,在容器发生变动时我们可以通过 ApplicationEvent 的子类通知到 ApplicationListener 接口的实现类,做对应的处理。例如,ApplicationContext 在启动、停止、关闭和刷新 20 时,分别会发出 ContextStartedEvent、ContextStoppedEvent、ContextClosedEvent 和 ContextRefreshedEvent 事件,这些事件就让我们有机会感知当前容器的状态。
如下两种方式可以进行自定义事件的发布和监听。
方式一:实现接口发布、监听事件
事件监听 实现
ApplicationListener
接口1
2
3
4
5
6
7
8
9
10
11
12
13
14package cn.probiecoder.fund.event;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class AgreeEventListener implements ApplicationListener<AgreeEvent> {
@Override
public void onApplicationEvent(AgreeEvent event) {
System.out.println("我是监听器");
System.out.println(event.getMessage());
}
}事件发布 实现
ApplicationEventPublisherAware
接口
1 |
|
方式二、使用注解
发布事件:
1 |
|
使用注解@EventListener
监听事件:
1 |
|
自定义事件:
1 |
|
Spring自定义事件
https://probiecoder.cn/spring/event.html