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来反射执行的。整个顺序如下:
在看看EventPublishingRunListener类:
public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {// 通过SpringApplication来获取所有的ApplicationListenerprivate 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);}}@Overridepublic void starting() {// multicastEvent执行所有ApplicationListener接口方法this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));}@Overridepublic void environmentPrepared(ConfigurableEnvironment environment) {// multicastEvent执行所有ApplicationListener接口方法this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));}@Overridepublic void contextPrepared(ConfigurableApplicationContext context) {// multicastEvent执行所有ApplicationListener接口方法this.initialMulticaster.multicastEvent(new ApplicationContextInitializedEvent(this.application, this.args, context));}@Overridepublic 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项目中ApplicationListener被EventPublishingRunListener implements SpringApplicationRunListener代理执行了
整个调用方式如下图:
大概就这样。回头看多一点再来补充