1. 程式人生 > >Springboot內建ApplicationListener

Springboot內建ApplicationListener

概述

本文所涉及程式碼基於 spring-boot 2.1.0

ApplicationListener是Spring框架提供的一個用於監聽應用事件(application event)的事件監聽器。它繼承自Java標準觀察者模式的EventListener介面。從Spring 3.0之後,一個ApplicationListener實現可以宣告自己所關心的事件型別。當一個ApplicationListener被註冊到Spring ApplicationContext之後,應用執行時應用事件會陸續發生,對應響應型別的事件監聽器會被呼叫。

介面ApplicationListener

定義如下 :

// 泛型介面,E定義了監聽器實現類所真正關心的應用事件型別
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

	/**
	 * Handle an application event.
	 * @param event the event to respond to
	 */
	void onApplicationEvent(E event);

}

分析

當Springboot應用程式啟動時,它會載入一組內建的ApplicationListener

:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	this.resourceLoader = resourceLoader;
	Assert.notNull(primarySources, "PrimarySources must not be null");
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
	this.webApplicationType =
WebApplicationType.deduceFromClasspath(); setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//<==== this.mainApplicationClass = deduceMainApplicationClass(); }

最終這些ApplicationListener會在下面位置被使用 :

public ConfigurableApplicationContext run(String... args) {
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	ConfigurableApplicationContext context = null;
	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
	configureHeadlessProperty();
	SpringApplicationRunListeners listeners = getRunListeners(args);<=====
	listeners.starting();
	// ... 省略其他程式碼
}

這些內建的ApplicationListener是:

名稱 介紹
org.sf.boot.ClearCachesApplicationListener 應用上下文載入完成後對快取做清除工作,響應事件ContextRefreshedEvent
org.sf.boot.builder.ParentContextCloserApplicationListener 監聽雙親應用上下文的關閉事件並往自己的孩子應用上下文中傳播,相關事件ParentContextAvailableEvent/ContextClosedEvent
org.sf.boot.context.FileEncodingApplicationListener 如果系統檔案編碼和環境變數中指定的不同則終止應用啟動。
具體的方法是比較系統屬性file.encoding和環境變數spring.mandatory-file-encoding是否相等(大小寫不敏感)。
org.sf.boot.context.config.AnsiOutputApplicationListener 根據spring.output.ansi.enabled引數配置AnsiOutput
org.sf.boot.context.config.ConfigFileApplicationListener EnvironmentPostProcessor,從常見的那些約定的位置讀取配置檔案,比如從以下目錄讀取application.properties,application.yml等配置檔案:
classpath:
file:.
classpath:config
file:./config/:
也可以配置成從其他指定的位置讀取配置檔案
org.sf.boot.context.config.DelegatingApplicationListener 監聽到事件後轉發給環境變數context.listener.classes指定的那些事件監聽器
org.sf.boot.context.logging.ClasspathLoggingApplicationListener 一個SmartApplicationListener,對環境就緒事件ApplicationEnvironmentPreparedEvent/應用失敗事件ApplicationFailedEvent做出響應,往日誌DEBUG級別輸出TCCL(thread context class loader)的classpath。
org.sf.boot.context.logging.LoggingApplicationListener 配置LoggingSystem。使用logging.config環境變數指定的配置或者預設配置
org.sf.boot.liquibase.LiquibaseServiceLocatorApplicationListener 使用一個可以和Spring Boot可執行jar包配合工作的版本替換liquibase ServiceLocator
org.sf.boot.autoconfigure.BackgroundPreinitializer 儘早觸發一些耗時的初始化任務,使用一個後臺執行緒

上表中包名springframework縮寫為sf