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.ConfigFileApplicationListenerEnvironmentPostProcessor,从常见的那些约定的位置读取配置文件,比如从以下目录读取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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值