文章目录
所有调试均使用SpringBoot 2.4.5版本。
ApplicationListener事件监听机制其实是由Spring提供的,应用内部的事件驱动机制。也就是Pub/Sub发布订阅机制在应用内部的实现。一般主要是用于监控应用内部的一些运行状况,在应用开发中也可以使用。
具体的实现机制可以到Spring中去探究,这里就来简单理解下SpringBoot对这个事件驱动机制做了哪些封装。
一、事件监听器使用
1、自己实现一个事件监听器
首先创建一个自定义的事件监听器:
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
System.out.println("======>MyApplicationListener: "+applicationEvent);
}
}
然后还是在项目的spring.factories中配置监听器
org.springframework.context.ApplicationListener=\
com.roy.applicationListener.MyApplicationListener
然后配置启动类。在启动类中发布一个自己的事件。
@SpringBootApplication
public class P1Application implements CommandLineRunner {
public static void main(String[] args) {
final SpringApplication application = new SpringApplication(P1Application.class);
// application.addInitializers(new MyApplicationContextInitializer());
application.run(args);
}
@Autowired
private ApplicationContext applicationContext;
@Override
public void run(String... args) throws Exception {
//自行发布一个事件。
applicationContext.publishEvent(new ApplicationEvent("selfEvent") {
});
}
}
正常启动SpringBoot应用,就能打印出启动过程中的关键事件的日志。这里把关键的事件日志给整理出来:
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationStartingEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationContextInitializedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationPreparedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationStartedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.availability.AvailabilityChangeEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
======>MyApplicationListener: com.roy.P1Application$1[source=selfEvent] ##!!自行发布的事件。
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationReadyEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.availability.AvailabilityChangeEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
======>MyApplicationListener: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
这里就打印出了SpringBoot应用启动过程中的多个内部事件。实际上这多个内部事件也就对应了启动过程的各个阶段,是梳理SpringBoot启动流程非常好的入口。
Spring事件机制的其他细节这里就不多说了,大家可以自行了解。我们这里还是专注于SpringBoot的部分。
2、事件监听器的其他配置方式:
这个事件监听机制是Spring非常重要的一个机制,有非常多的配置方式。除了上面提到的基于spring.factories文件配置的方式,还有其他几种配置方式。
2.1 SpringApplication.addListener
跟之前的Initializer一样,这个事件监听器也可以在SpringApplication中直接添加。
@SpringBootApplication
public class P1Application implements CommandLineRunner {
public static void main(String[] args) {
final SpringApplication application = new SpringApplication(P1Application.class);
//添加事件监听器
application.addListeners(new MyApplicationListener());
application.run(args);
}
}
2.2 基于注解添加
基于注解,将MyApplicationListener配置到Spring的IOC容器中。
@Configuration
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
System.out.println("======>MyApplicationListener: "+applicationEvent);
}
}
2.3 在SpringBoot的配置文件中配置
另外还一种方式,可以在SpringBoot的配置文件application.properties中配置
context.listener.classes=com.roy.applicationListener.MyApplicationListener
这几种方式都可以配置事件监听器。另外,其实在上一章节ApplicationContextInitializer中也能看到,在SpringBoot内部也在应用初始化中扩展出了很多通过application添加事件监听器的扩展。
二、核心机制解读
事件机制的使用方式很多,不同的配置方式也有不同的加载流程。我们这里还是只解读SpringBoot中如何通过spring.factories文件来加载事件监听器的。
SpringBoot中对于监听器的处理,也跟ApplicationContextInitializer的处理流程是差不多的。首先在SpringApplication的构造方法中加载所有的监听器:
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
...
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
//加载spring.facotries中注册的所有事件监听器
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
...
}
然后在SpringApplication的run方法中启动所有监听器:
public ConfigurableApplicationContext run(String..

最低0.47元/天 解锁文章
8004

被折叠的 条评论
为什么被折叠?



