微信公众号:吉姆餐厅ak 学习更多源码知识,欢迎关注。 spring
![]()
SpringBoot2 | SpringBoot启动流程源码分析(一)bash
SpringBoot2 | SpringBoot启动流程源码分析(二)微信
SpringBoot2 | @SpringBootApplication注解 自动化配置流程源码分析(三)app
SpringBoot2 | SpringBoot Environment源码分析(四)框架
SpringBoot2 | SpringBoot自定义AutoConfiguration | SpringBoot自定义starter(五)less
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)ide
SpringBoot2 | 条件注解@ConditionalOnBean原理源码深度解析(七)工具
SpringBoot2 | Spring AOP 原理源码深度剖析(八)源码分析
SpringBoot2 | SpingBoot FilterRegistrationBean 注册组件 | FilterChain 责任链源码分析(九)学习
SpringBoot2 | BeanDefinition 注册核心类 ImportBeanDefinitionRegistrar (十)
SpringBoot2 | Spring 核心扩展接口 | 核心扩展方法总结(十一)
咱们都知道Spring源码博大精深,阅读起来相对困难。缘由之一就是内部用了大量的监听器,spring相关的框架,皆是如此,spring security,springBoot
等。今天来看下springBoot
监听器的应用。
由于springBoot是对spring的封装,因此springBoot的监听器的应用主要是在启动模块。
springBoot
监听器的主要分为两类:
1)运行时监听器
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
复制代码
2)上下文监听器
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\
org.springframework.boot.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.logging.LoggingApplicationListener
复制代码
注意:springBoot运行时监听器做用是用来触发springBoot上下文监听器,再根据各监听器监听的事件进行区分。 上面默认监听器的做用以下:
运行时监听器和上下文监听器都是定义在
spring.factories
文件中。
启动流程前面两篇已经有详细分析,因此本篇咱们只看监听器相关逻辑。
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
configureHeadlessProperty();
//获取启动监听器的监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
//用该监听器来启动全部监听器
listeners.started();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
context = createAndRefreshContext(listeners, applicationArguments);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, ex);
throw new IllegalStateException(ex);
}
}
复制代码
SpringApplicationRunListeners listeners = getRunListeners(args);
这里建立了一个关键类:SpringApplicationRunListeners
。
这是一个封装工具类,封装了全部的启动类监听器。默认只有一个实例,这里封装成List<SpringApplicationRunListener> listeners
,主要是方便咱们扩展,咱们能够定义本身的启动类监听器。
//启动类监听器
private final List<SpringApplicationRunListener> listeners;
SpringApplicationRunListeners(Log log,
Collection<? extends SpringApplicationRunListener> listeners) {
this.log = log;
this.listeners = new ArrayList<SpringApplicationRunListener>(listeners);
}
//启动上下文事件监听
public void started() {
for (SpringApplicationRunListener listener : this.listeners) {
listener.started();
}
}
//environment准备完毕事件监听
public void environmentPrepared(ConfigurableEnvironment environment) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
}
//spring上下文准备完毕事件监听
public void contextPrepared(ConfigurableApplicationContext context) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.contextPrepared(context);
}
}
//上下文配置类加载事件监听
public void contextLoaded(ConfigurableApplicationContext context) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.contextLoaded(context);
}
}
//上下文构造完成事件监听
public void finished(ConfigurableApplicationContext context, Throwable exception) {
for (SpringApplicationRunListener listener : this.listeners) {
callFinishedListener(listener, context, exception);
}
}
复制代码
在前面的启动流程源码分析中介绍过,这些方法会在合适的时间点触发执行,而后广播出不一样的事件。
跟进去EventPublishingRunListener
:
public EventPublishingRunListener(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
//spring事件机制通用的事件发布类
this.multicaster = new SimpleApplicationEventMulticaster();
for (ApplicationListener<?> listener : application.getListeners()) {
this.multicaster.addApplicationListener(listener);
}
}
复制代码
上面会默认建立全局的事件发布工具类SimpleApplicationEventMulticaster
。
@Override
public void started() {
publishEvent(new ApplicationStartedEvent(this.application, this.args));
}
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
publishEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args,
environment));
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
registerApplicationEventMulticaster(context);
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
for (ApplicationListener<?> listener : this.application.getListeners()) {
if (listener instanceof ApplicationContextAware) {
((ApplicationContextAware) listener).setApplicationContext(context);
}
context.addApplicationListener(listener);
}
publishEvent(new ApplicationPreparedEvent(this.application, this.args, context));
}
@Override
public void finished(ConfigurableApplicationContext context, Throwable exception) {
publishEvent(getFinishedEvent(context, exception));
}
复制代码
能够看出每一个方法都会发布不一样的事件,全部的事件统一继承SpringApplicationEvent
:
继续跟进事件广播方法:
@Override
public void multicastEvent(ApplicationEvent event) {
multicastEvent(event, resolveDefaultEventType(event));
}
@Override
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
//根据事件类型选取须要通知的监听器
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
//获取线程池,若是为null,则同步执行
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
@Override
public void run() {
invokeListener(listener, event);
}
});
}
else {
invokeListener(listener, event);
}
}
}
复制代码
重点来看一下根据类型获取监听器:getApplicationListeners(event, type)
跟进该方法:
private Collection<ApplicationListener<?>> retrieveApplicationListeners(
ResolvableType eventType, Class<?> sourceType, ListenerRetriever retriever) {
//......
//根据类型匹配监听器
for (ApplicationListener<?> listener : listeners) {
if (supportsEvent(listener, eventType, sourceType)) {
if (retriever != null) {
retriever.applicationListeners.add(listener);
}
allListeners.add(listener);
}
}
//......
AnnotationAwareOrderComparator.sort(allListeners);
return allListeners;
}
复制代码
上面省去了一些不相关代码,继续跟进:supportsEvent(listener, eventType, sourceType)
:
protected boolean supportsEvent(ApplicationListener<?> listener, ResolvableType eventType, Class<?> sourceType) {
//判断监听器是不是 GenericApplicationListener 的子类,若是不是就返回一个GenericApplicationListenerAdapter
GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener ?
(GenericApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
}
复制代码
public interface GenericApplicationListener extends ApplicationListener<ApplicationEvent>, Ordered {
boolean supportsEventType(ResolvableType eventType);
boolean supportsSourceType(Class<?> sourceType);
}
复制代码
这里又出现一个关键类:GenericApplicationListener
,该类是 spring 提供的用于重写匹配监听器事件的接口。 就是说若是须要判断的监听器是GenericApplicationListener
的子类,说明类型匹配方法已被重现,就调用子类的匹配方法。若是不是,则为咱们提供一个默认的适配器用来匹配:GenericApplicationListenerAdapter
:
继续跟进该类的supportsEventType(ResolvableType eventType)
方法:
public boolean supportsEventType(ResolvableType eventType) {
if (this.delegate instanceof SmartApplicationListener) {
Class<? extends ApplicationEvent> eventClass = (Class<? extends ApplicationEvent>) eventType.getRawClass();
return ((SmartApplicationListener) this.delegate).supportsEventType(eventClass);
}
else {
return (this.declaredEventType == null || this.declaredEventType.isAssignableFrom(eventType));
}
}
复制代码
能够看到该类最终调用的是declaredEventType.isAssignableFrom(eventType)
方法,也就是说,若是咱们没有重写监听器匹配方法,那么发布的事件 event 会被监听 event以及监听event的父类的监听器监听到。
/**
* Created by zhangshukang on 2018/9/22.
*/
public class SimpleApplicationListener implements GenericApplicationListener,ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("myApplistener execute...");
}
@Override
public boolean supportsEventType(ResolvableType eventType) {
return true;
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
@Override
public int getOrder() {
return 0;
}
复制代码
上面supportsEventType
和supportsSourceType
是预留的扩展方法,这里所有为true,也就意味着监听全部的ApplicationEvent
事件,方法会执行屡次:
springBoot
总体框架就是经过该监听器org.springframework.boot.SpringApplicationRunListeners
,来触发上下文监听器。经过上下文监听器来完成总体逻辑,好比加载配置文件,加载配置类,初始化日志环境等。
SpringBoot2 | SpringBoot启动流程源码分析(一)
SpringBoot2 | SpringBoot启动流程源码分析(二)
SpringBoot2 | @SpringBootApplication注解 自动化配置流程源码分析(三)
SpringBoot2 | SpringBoot Environment源码分析(四)
SpringBoot2 | SpringBoot自定义AutoConfiguration | SpringBoot自定义starter(五)