版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andy_zhang2007/article/details/84105284
概述
本文所涉及代码基于 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