ApplicationListener类初始化位置
在类SpringApplication的构造方法,第267行

在META-INFO/spring.factories中配置的实现类
spring-boot
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener
spring-boot-autoconfigure
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer
相关类

ApplicationEvent事件祖先类org.springframework.context.event.ApplicationEventMulticaster事件多播器org.springframework.context.event.SmartApplicationListener监听器org.springframework.context.event.GenericApplicationListener监听器org.springframework.context.event.EventListener监听器
SpringBoot启动过程中的事件,都由org.springframework.context.event.ApplicationEventMulticaster播出去,让监听器处理。
监听器触发逻辑
从加载开始
入口在SpringApplication的构造方法,第267行
类工厂加载ApplicationListener的实现类
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
然后在SpringApplication的实例方法run中,第297行,加载SpringApplicationRunListeners类,这个类中持有多播器的引用。
SpringApplicationRunListeners listeners = getRunListeners(args);
SpringApplicationRunListeners实例化的时候,会通过工厂类加载SpringApplicationRunListener
private SpringApplicationRunListeners getRunListeners(String[] args) {Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };return new SpringApplicationRunListeners(logger,getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args),this.applicationStartup);
}
SpringApplicationRunListener的实现类
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
EventPublishingRunListener中持有的监听器,就是SpringApplication中加载的ApplicationListener的实现类
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);}
}
ClearCachesApplicationListenerParentContextCloserApplicationListenerFileEncodingApplicationListenerAnsiOutputApplicationListenerDelegatingApplicationListenerLoggingApplicationListenerEnvironmentPostProcessorApplicationListenerBackgroundPreinitializer
存储
然后这些监听器被放入了SimpleApplicationEventMulticaster简单多播器里面
@Override
public void addApplicationListener(ApplicationListener<?> listener) {synchronized (this.defaultRetriever) {// Explicitly remove target for a proxy, if registered already,// in order to avoid double invocations of the same listener.Object singletonTarget = AopProxyUtils.getSingletonTarget(listener);if (singletonTarget instanceof ApplicationListener) {this.defaultRetriever.applicationListeners.remove(singletonTarget);}this.defaultRetriever.applicationListeners.add(listener);this.retrieverCache.clear();}
}
默认的defaultRetriever则是一个内部类DefaultListenerRetriever

触发
会在多个地方触发,比如
SpringApplication的run方法,第298行
listeners.starting(bootstrapContext, this.mainApplicationClass);
触发过程又涉及到事件的设计