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);
觸發過程又涉及到事件的設計