欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ApplicationListener和SpringApplicationRunListener的联系

时间:2023-08-01
前言

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来获取所有的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代理执行了

整个调用方式如下图:

大概就这样。回头看多一点再来补充

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。