Spring 提供了许多非常好用的机制,比如IOC,AOP。这些几乎在所有的Spring项目中都有广泛的使用,这里讲解的是Spring提供的事件发布订阅机制,掌握发布订阅设计模式可以更好的在项目中对功能进行设计,也多一种解决方案。同时如果你掌握了SpringBoot的事件发布的全部流程,你就掌握了SpringBoot在整个启动过程中干了什么事,走了哪些流程
事件类
scala 代码解读复制代码public class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
订阅类
typescript 代码解读复制代码@Component
public class MyEventSubscribe implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
String msg = (String) event.getSource();
System.out.println("我接受到了事件,msg: "+msg);
}
}
发布类
java 代码解读复制代码@Component
public class MyTest implements CommandLineRunner {
@Autowired
ApplicationEventPublisher applicationEventPublisher;
@Override
public void run(String... args) throws Exception {
applicationEventPublisher.publishEvent(new MyEvent("hello event"));
}
}
Log输出
vbnet 代码解读复制代码2024-09-25 14:44:23.852 INFO 22492 --- [ main] c.c.s.SpringResCode1Application : Starting SpringResCode1Application using Java 1.8.0_202 on TCN1214966 with PID 22492 (D:\code\java\springResCode1\target\classes started by changtao.deng in D:\code\java\springResCode1)
2024-09-25 14:44:23.858 INFO 22492 --- [ main] c.c.s.SpringResCode1Application : No active profile set, falling back to 1 default profile: "default"
我接受到了事件,msg: hello event
2024-09-25 14:44:24.555 INFO 22492 --- [ main] c.c.s.SpringResCode1Application : Started SpringResCode1Application in 1.243 seconds (JVM running for 2.426)
除了自定义事件以外,你还可以监听SpringBoot 应用在启动过程中的发布的事件
typescript 代码解读复制代码@Component
public class MyEventSubscribe {
@EventListener
public void handleMyEvent1(ApplicationStartingEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ApplicationStartingEvent事件,msg: " + source);
}
@EventListener
public void handleMyEvent2(ApplicationEnvironmentPreparedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ApplicationEnvironmentPreparedEvent事件,msg: " + source);
}
@EventListener
public void handleMyEvent3(ApplicationContextInitializedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ApplicationContextInitializedEvent事件,msg: " + source);
}
@EventListener
public void handleMyEvent4(ApplicationPreparedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ApplicationPreparedEvent事件,msg: " + source);
}
@EventListener
public void handleMyEvent6(ContextRefreshedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ContextRefreshed事件,msg: " + source);
}
@EventListener
public void handleMyEvent5(ApplicationStartedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ApplicationStartedEvent事件,msg: " + source);
}
@EventListener
public void handleMyEvent7(ContextClosedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ContextClosed事件,msg: " + source);
}
@EventListener
public void handleMyEvent8(ContextStoppedEvent event) {
Object source = event.getSource();
System.out.println("我接受到了ContextStopped事件,msg: " + source);
}
}
我这里还不是完全列举就写了八个事件,还是比较多的,那让我们执行下
vbnet 代码解读复制代码2024-09-25 15:20:39.298 INFO 12724 --- [ main] c.c.s.SpringResCode1Application : No active profile set, falling back to 1 default profile: "default"
我接受到了ContextRefreshed事件,msg: org.springframework.context.annotation.AnnotationConfigApplicationContext@14cd1699, started on Wed Sep 25 15:20:39 CST 2024
2024-09-25 15:20:40.093 INFO 12724 --- [ main] c.c.s.SpringResCode1Application : Started SpringResCode1Application in 1.272 seconds (JVM running for 2.278)
我接受到了ApplicationStartedEvent事件,msg: org.springframework.boot.SpringApplication@5f0e9815
我接受到了ContextClosed事件,msg: org.springframework.context.annotation.AnnotationConfigApplicationContext@14cd1699, started on Wed Sep 25 15:20:39 CST 2024
Process finished with exit code 0
并没有全部触发,只触发了ContextRefreshed,ApplicationStarted 以及最后的ContextClosed。
这里就先简单讲下这些事件
ApplicationStartingEvent
:
ApplicationEnvironmentPreparedEvent
:
ApplicationContext
之前。这时,Environment
已经准备好,可以用于配置和处理。ApplicationContextInitializedEvent
:
ApplicationContext
初始化完成后发送,但在刷新之前。此时,所有的 ApplicationContextInitializer
已经被调用。ApplicationPreparedEvent
:
ApplicationContext
准备完成后发送,但在刷新之前。此时,所有的 Bean 定义已经加载,但尚未实例化。ApplicationStartedEvent
:
ApplicationContext
刷新并启动完成后发送。这标志着应用程序已经完全启动并准备好处理请求。ContextRefreshedEvent
:
ApplicationContext
完成刷新时发送。此时,所有的单例 Bean 已经被实例化并且已完成初始化。ContextStoppedEvent
:
ApplicationContext
停止时发送。此事件需要显式调用 stop()
方法。ContextClosedEvent
:
ApplicationContext
关闭时发送。这通常在 JVM 关闭或显式调用 close()
方法时发生。因为我们的Bean是通过@Component注解来进行IOC注入的,所以上下文没有完成所有的Bean注入前的事件这个监听器是监听不到的,也就是ApplicationStartedEvent 之前的事件无法监听到。那有没有办法监听更前面的事件呢,其实也有,那就是通过SPI的方式进行注入,因为SPI的注入会在SpringContext的构造方法中就进行执行。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。