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);
}
}
ClearCachesApplicationListener
ParentContextCloserApplicationListener
FileEncodingApplicationListener
AnsiOutputApplicationListener
DelegatingApplicationListener
LoggingApplicationListener
EnvironmentPostProcessorApplicationListener
BackgroundPreinitializer
存储
然后这些监听器被放入了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);
触发过程又涉及到事件的设计