前言 ApplicationListener接口
属于package org.springframework.context;
SpringApplicationRunListener接口
属于package org.springframework.boot;
从《SpringBoot技术内幕》一书中得知:
ApplicationContext通过ApplicationListener监听ApplicationEvent该书是在介绍SpringBoot项目中的
SpringApplication.run()
方法的构造方法
时讲的,SpringApplication
的构造方法如下: public SpringApplication(ResourceLoader resourceLoader, Class>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 传app类型,有NONE、SERVLET、REACTIVE三种
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 设置ApplicationContextInitializer,加载classpath的META-INF文件夹的spring.fatories文件中找以ApplicationContextInitializer.class为key的value类
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 设置ApplicationListener,加载classpath的META-INF文件夹的spring.fatories文件中找以ApplicationListener.class为key的value类
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
但是,我看了看源码,只找到了
ApplicationListener
的创建,却看不到调用,然后强迫症就犯了,得找出来。于是,我做了下面的实验。
实验
文章图片
这两个自定义类代码如下:
public class MyApplicationListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("MyApplicationListener执行了监听到了"+event.getClass().getSimpleName());
}
}
public class MySpringApplicationRunListener implements SpringApplicationRunListener, PriorityOrdered {// 必须有这个签名的构造方法,否则会报错
public MySpringApplicationRunListener(SpringApplication application, String[] args) {
System.out.println("MySpringApplicationRunListener 构造方法执行");
}
@Override
public void starting() {
System.out.println("MySpringApplicationRunListener starting方法执行");
}
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
System.out.println("MySpringApplicationRunListener environmentPrepared方法执行");
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener contextPrepared方法执行");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener contextLoaded方法执行");
}
@Override
public void started(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener started方法执行");
}
@Override
public void running(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener running方法执行");
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("MySpringApplicationRunListener failed方法执行");
}
@Override
public int getOrder() {
return 0;
}
}
执行结果如下:
文章图片
理解 先来看看这两个接口的注释:
ApplicationListener:
Interface to be implemented by application event listeners.
Based on the standard java.util.EventListener interface for the Observer design pattern.
As of Spring 3.0, an ApplicationListener can generically declare the event type that it is interested in. When registered with a Spring ApplicationContext, events will be filtered accordingly, with the listener getting invoked for matching event objects only.
SpringApplicationRunListener:
Listener for the SpringApplication run method. SpringApplicationRunListeners are loaded via the SpringFactoriesLoader and should declare a public constructor that accepts a SpringApplication instance and a String[] of arguments. A new SpringApplicationRunListener instance will be created for each run.可以看到,
ApplicationListener:
属于context
包,应用更加广泛;而SpringApplicationRunListener
只是Listener for the SpringApplication
run method。通过调试也可以发现,ApplicationListener不是在SpringBoot项目中直接调用执行的,而是通过
org.springframework.boot.context.event.EventPublishingRunListener implements SpringApplicationRunListener
类中的成员SimpleApplicationEventMulticaster
来反射执行的。整个顺序如下:SpringApplication构造函数
从spring.fatories
中加载所有的ApplicationListener
。- 在
SpringApplication
的run
方法中 从spring.fatories
中加载所有SpringApplicationRunListener
,这其中会加载了一个org.springframework.boot.context.event.EventPublishingRunListener implements SpringApplicationRunListener
。将步骤1中所有的ApplicationListener
通过构造方法传给EventPublishingRunListener
。 - 看下图两张图,一个是run方法代码,一个是
SpringApplicationRunListener
的几个接口方法在*run*
方法的生命周期的触发时机。
文章图片
文章图片
EventPublishingRunListener
类:public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {
// 通过SpringApplication来获取所有的ApplicationListener
private final SpringApplication application;
private final String[] args;
// 真正ApplicationListener的执行者
private final SimpleApplicationEventMulticaster initialMulticaster;
public EventPublishingRunListener(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
this.initialMulticaster = new SimpleApplicationEventMulticaster();
// 重点代码
for (ApplicationListener> listener : application.getListeners()) {
this.initialMulticaster.addApplicationListener(listener);
}
}
@Override
public void starting() {
// multicastEvent执行所有ApplicationListener接口方法
this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
} @Override
public void environmentPrepared(ConfigurableEnvironment environment) {
// multicastEvent执行所有ApplicationListener接口方法
this.initialMulticaster
.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
} @Override
public void contextPrepared(ConfigurableApplicationContext context) {
// multicastEvent执行所有ApplicationListener接口方法
this.initialMulticaster
.multicastEvent(new ApplicationContextInitializedEvent(this.application, this.args, context));
} @Override
public void contextLoaded(ConfigurableApplicationContext context) {
for (ApplicationListener> listener : this.application.getListeners()) {
if (listener instanceof ApplicationContextAware) {
((ApplicationContextAware) listener).setApplicationContext(context);
}
context.addApplicationListener(listener);
}
// multicastEvent执行所有ApplicationListener接口方法
this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context));
} ...}
也就是说,在SpringBoot项目中【java|ApplicationListener和SpringApplicationRunListener的联系】整个调用方式如下图:ApplicationListener
被EventPublishingRunListener implements SpringApplicationRunListener
代理执行了
文章图片
大概就这样。回头看多一点再来补充
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])