SpringBoot内置生命周期事件详解 SpringBoot源码(十)
SpringBoot中文注释项目Github地址:
https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE
本篇接 SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
1 温故而知新
温故而知新,我们来简单回顾一下上篇的内容,上一篇我们分析了SpringBoot启动时广播生命周期事件的原理,现将关键步骤再浓缩总结下:
- 为广播SpringBoot内置生命周期事件做前期准备:1)首先加载
ApplicationListener
监听器实现类;2)其次加载SPI扩展类EventPublishingRunListener
。 - SpringBoot启动时利用
EventPublishingRunListener
广播生命周期事件,然后ApplicationListener
监听器实现类监听相应的生命周期事件执行一些初始化逻辑的工作。
2 引言
上篇文章的侧重点是分析了SpringBoot启动时广播生命周期事件的原理,此篇文章我们再来详细分析SpringBoot内置的7种生命周期事件的源码。
3 SpringBoot生命周期事件源码分析
分析SpringBoot的生命周期事件,我们先来看一张类结构图:
由上图可以看到事件类之间的关系:
- 最顶级的父类是JDK的事件基类
EventObject
; - 然后Spring的事件基类
ApplicationEvent
继承了JDK的事件基类EventObject
; - 其次SpringBoot的生命周期事件基类
SpringApplicationEvent
继承了Spring的事件基类ApplicationEvent
; - 最后SpringBoot具体的7个生命周期事件类再继承了SpringBoot的生命周期事件基类
SpringApplicationEvent
。
3.1 JDK的事件基类EventObject
EventObject
类是JDK的事件基类,可以说是所有Java事件类的基本,即所有的Java事件类都直接或间接继承于该类,源码如下:
// EventObject.java
public class EventObject implements java.io.Serializable {
private static final long serialVersionUID = 5516075349620653480L;
/**
* The object on which the Event initially occurred.
*/
protected transient Object source;
/**
* Constructs a prototypical Event.
*
* @param source The object on which the Event initially occurred.
* @exception IllegalArgumentException if source is null.
*/
public EventObject(Object source) {
if (source == null)
throw new IllegalArgumentException("null source");
this.source = source;
}
/**
* The object on which the Event initially occurred.
*
* @return The object on which the Event initially occurred.
*/
public Object getSource() {
return source;
}
/**
* Returns a String representation of this EventObject.
*
* @return A a String representation of this EventObject.
*/
public String toString() {
return getClass().getName() + "[source=" + source + "]";
}
}
可以看到EventObject
类只有一个属性source
,这个属性是用来记录最初事件是发生在哪个类,举个栗子,比如在SpringBoot启动过程中会发射ApplicationStartingEvent
事件,而这个事件最初是在SpringApplication
类中发射的,因此source
就是SpringApplication
对象。
3.2 Spring的事件基类ApplicationEvent
ApplicationEvent
继承了DK的事件基类EventObject
类,是Spring的事件基类,被所有Spring的具体事件类继承,源码如下:
// ApplicationEvent.java
/**
* Class to be extended by all application events. Abstract as it
* doesn't make sense for generic events to be published directly.
*
* @author Rod Johnson
* @author Juergen Hoeller
*/
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened. */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}
可以看到ApplicationEvent
有且仅有一个属性timestamp
,该属性是用来记录事件发生的时间。
3.3 SpringBoot的事件基类SpringApplicationEvent
SpringApplicationEvent
类继承了Spring的事件基类ApplicationEvent
,是所有SpringBoot内置生命周期事件的父类,源码如下:
/**
* Base class for {@link ApplicationEvent} related to a {@link SpringApplication}.
*
* @author Phillip Webb
*/
@SuppressWarnings("serial")
public abstract class SpringApplicationEvent extends ApplicationEvent {
private final String[] args;
public SpringApplicationEvent(SpringApplication application, String[] args) {
super(application);
this.args = args;
}
public SpringApplication getSpringApplication() {
return (SpringApplication) getSource();
}
public final String[] getArgs() {
return this.args;
}
}
可以看到SpringApplicationEvent
有且仅有一个属性args
,该属性就是SpringBoot启动时的命令行参数即标注@SpringBootApplication
启动类中main
函数的参数。
3.4 SpringBoot具体的生命周期事件类
接下来我们再来看一下SpringBoot
内置生命周期事件即SpringApplicationEvent
的具体子类们。
3.4.1 ApplicationStartingEvent
// ApplicationStartingEvent.java
public class ApplicationStartingEvent extends SpringApplicationEvent {
public ApplicationStartingEvent(SpringApplication application, String[] args) {
super(application, args);
}
}
SpringBoot开始启动时便会发布ApplicationStartingEvent
事件,其发布时机在环境变量Environment或容器ApplicationContext创建前但在注册ApplicationListener
具体监听器之后,标志标志SpringApplication
开始启动。
3.4.2 ApplicationEnvironmentPreparedEvent
// ApplicationEnvironmentPreparedEvent.java
public class ApplicationEnvironmentPreparedEvent extends SpringApplicationEvent {
private final ConfigurableEnvironment environment;
/**
* Create a new {@link ApplicationEnvironmentPreparedEvent} instance.
* @param application the current application
* @param args the arguments the application is running with
* @param environment the environment that was just created
*/
public ApplicationEnvironmentPreparedEvent(SpringApplication application,
String[] args, ConfigurableEnvironment environment) {
super(application, args);
this.environment = environment;
}
/**
* Return the environment.
* @return the environment
*/
public ConfigurableEnvironment getEnvironment() {
return this.environment;
}
}
可以看到ApplicationEnvironmentPreparedEvent
事件多了一个environment
属性,我们不妨想一下,多了environment
属性的作用是啥?
答案就是ApplicationEnvironmentPreparedEvent
事件的environment
属性作用是利用事件发布订阅机制,相应监听器们可以从ApplicationEnvironmentPreparedEvent
事件中取出environment
变量,然后我们可以为environment
属性增加属性值或读出environment
变量中的值。
举个栗子:
ConfigFileApplicationListener
监听器就是监听了ApplicationEnvironmentPreparedEvent
事件,然后取出ApplicationEnvironmentPreparedEvent
事件的environment
属性,然后再为environment
属性增加application.properties
配置文件中的环境变量值。
当SpringApplication已经开始启动且环境变量Environment
已经创建后,并且为环境变量Environment
配置了命令行和Servlet
等类型的环境变量后,此时会发布ApplicationEnvironmentPreparedEvent
事件。
监听ApplicationEnvironmentPreparedEvent
事件的第一个监听器是ConfigFileApplicationListener
,因为是ConfigFileApplicationListener
监听器还要为环境变量Environment
增加application.properties
配置文件中的环境变量;此后还有一些也是监听ApplicationEnvironmentPreparedEvent
事件的其他监听器监听到此事件时,此时可以说环境变量Environment
几乎已经完全准备好了。
思考: 监听同一事件的监听器们执行监听逻辑时是有顺序的,我们可以想一下这个排序逻辑是什么时候排序的?还有为什么要这样排序呢?
3.4.3 ApplicationContextInitializedEvent
// ApplicationContextInitializedEvent.java
public class ApplicationContextInitializedEvent extends SpringApplicationEvent {
private final ConfigurableApplicationContext context;
/**
* Create a new {@link ApplicationContextInitializedEvent} instance.
* @param application the current application
* @param args the arguments the application is running with
* @param context the context that has been initialized
*/
public ApplicationContextInitializedEvent(SpringApplication application,
String[] args, ConfigurableApplicationContext context) {
super(application, args);
this.context = context;
}
/**
* Return the application context.
* @return the context
*/
public ConfigurableApplicationContext getApplicationContext() {
return this.context;
}
}
可以看到ApplicationContextInitializedEvent
事件多了个ConfigurableApplicationContext
类型的context
属性,context
属性的作用同样是为了相应监听器可以拿到这个context
属性执行一些逻辑,具体作用将在3.4.4
详述。
ApplicationContextInitializedEvent
事件在ApplicationContext
容器创建后,且为ApplicationContext
容器设置了environment
变量和执行了ApplicationContextInitializers
的初始化方法后但在bean定义加载前触发,标志ApplicationContext已经初始化完毕。
扩展: 可以看到
ApplicationContextInitializedEvent
是在为context
容器配置environment
变量后触发,此时ApplicationContextInitializedEvent
等事件只要有context
容器的话,那么其他需要environment
环境变量的监听器只需要从context
中取出environment
变量即可,从而ApplicationContextInitializedEvent
等事件没必要再配置environment
属性。
3.4.4 ApplicationPreparedEvent
// ApplicationPreparedEvent.java
public class ApplicationPreparedEvent extends SpringApplicationEvent {
private final ConfigurableApplicationContext context;
/**
* Create a new {@link ApplicationPreparedEvent} instance.
* @param application the current application
* @param args the arguments the application is running with
* @param context the ApplicationContext about to be refreshed
*/
public ApplicationPreparedEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context) {
super(application, args);
this.context = context;
}
/**
* Return the application context.
* @return the context
*/
public ConfigurableApplicationContext getApplicationContext() {
return this.context;
}
}
同样可以看到ApplicationPreparedEvent
事件多了个ConfigurableApplicationContext
类型的context
属性,多了context
属性的作用是能让监听该事件的监听器们能拿到context
属性,监听器拿到context
属性一般有如下作用:
- 从事件中取出
context
属性,然后可以增加一些后置处理器,比如ConfigFileApplicationListener
监听器监听到ApplicationPreparedEvent
事件后,然后取出context
变量,通过context
变量增加了PropertySourceOrderingPostProcessor
这个后置处理器; - 通过
context
属性取出beanFactory
容器,然后注册一些bean
,比如LoggingApplicationListener
监听器通过ApplicationPreparedEvent
事件的context
属性取出beanFactory
容器,然后注册了springBootLoggingSystem
这个单例bean
; - 通过
context
属性取出Environment
环境变量,然后就可以操作环境变量,比如PropertiesMigrationListener
。
ApplicationPreparedEvent
事件在ApplicationContext
容器已经完全准备好时但在容器刷新前触发,在这个阶段bean
定义已经加载完毕还有environment
已经准备好可以用了。
3.4.5 ApplicationStartedEvent
// ApplicationStartedEvent.java
public class ApplicationStartedEvent extends SpringApplicationEvent {
private final ConfigurableApplicationContext context;
/**
* Create a new {@link ApplicationStartedEvent} instance.
* @param application the current application
* @param args the arguments the application is running with
* @param context the context that was being created
*/
public ApplicationStartedEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context) {
super(application, args);
this.context = context;
}
/**
* Return the application context.
* @return the context
*/
public ConfigurableApplicationContext getApplicationContext() {
return this.context;
}
}
ApplicationStartedEvent
事件将在容器刷新后但ApplicationRunner
和CommandLineRunner
的run
方法执行前触发,标志Spring
容器已经刷新,此时容器已经准备完毕了。
扩展: 这里提到了
ApplicationRunner
和CommandLineRunner
接口有啥作用呢?我们一般会在Spring
容器刷新完毕后,此时可能有一些系统参数等静态数据需要加载,此时我们就可以实现了ApplicationRunner
或CommandLineRunner
接口来实现静态数据的加载。
3.4.6 ApplicationReadyEvent
// ApplicationReadyEvent.java
public class ApplicationReadyEvent extends SpringApplicationEvent {
private final ConfigurableApplicationContext context;
/**
* Create a new {@link ApplicationReadyEvent} instance.
* @param application the current application
* @param args the arguments the application is running with
* @param context the context that was being created
*/
public ApplicationReadyEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context) {
super(application, args);
this.context = context;
}
/**
* Return the application context.
* @return the context
*/
public ConfigurableApplicationContext getApplicationContext() {
return this.context;
}
}
ApplicationReadyEvent
事件在调用完ApplicationRunner
和CommandLineRunner
的run
方法后触发,此时标志SpringApplication
已经正在运行。
3.4.7 ApplicationFailedEvent
// ApplicationFailedEvent.java
public class ApplicationFailedEvent extends SpringApplicationEvent {
private final ConfigurableApplicationContext context;
private final Throwable exception;
/**
* Create a new {@link ApplicationFailedEvent} instance.
* @param application the current application
* @param args the arguments the application was running with
* @param context the context that was being created (maybe null)
* @param exception the exception that caused the error
*/
public ApplicationFailedEvent(SpringApplication application, String[] args,
ConfigurableApplicationContext context, Throwable exception) {
super(application, args);
this.context = context;
this.exception = exception;
}
/**
* Return the application context.
* @return the context
*/
public ConfigurableApplicationContext getApplicationContext() {
return this.context;
}
/**
* Return the exception that caused the failure.
* @return the exception
*/
public Throwable getException() {
return this.exception;
}
}
可以看到ApplicationFailedEvent
事件除了多了一个context
属性外,还多了一个Throwable
类型的exception
属性用来记录SpringBoot启动失败时的异常。
ApplicationFailedEvent
事件在SpringBoot启动失败时触发,标志SpringBoot启动失败。
4 小结
此篇文章相对简单,对SpringBoot内置的7种生命周期事件进行了详细分析。我们还是引用上篇文章的一张图来回顾一下这些生命周期事件及其用途:
5 写在最后
由于有一些小伙伴们建议之前有些源码分析文章太长,导致耐心不够,看不下去,因此,之后的源码分析文章如果太长的话,笔者将会考虑拆分为几篇文章,这样就比较短小了,比较容易看完,嘿嘿。
【源码笔记】Github地址:
https://github.com/yuanmabiji/Java-SourceCode-Blogs
点赞搞起来,嘿嘿嘿!
公众号【源码笔记】专注于Java后端系列框架的源码分析。
SpringBoot内置生命周期事件详解 SpringBoot源码(十)的更多相关文章
- 详解 QT 源码之 Qt 事件机制原理
QT 源码之 Qt 事件机制原理是本文要介绍的内容,在用Qt写Gui程序的时候,在main函数里面最后依据都是app.exec();很多书上对这句的解释是,使 Qt 程序进入消息循环.下面我们就到ex ...
- SpringBoot之DispatcherServlet详解及源码解析
在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...
- 线程池底层原理详解与源码分析(补充部分---ScheduledThreadPoolExecutor类分析)
[1]前言 本篇幅是对 线程池底层原理详解与源码分析 的补充,默认你已经看完了上一篇对ThreadPoolExecutor类有了足够的了解. [2]ScheduledThreadPoolExecut ...
- Android应用AsyncTask处理机制详解及源码分析
1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个知识点.前面我们分析了Handler异步机制原理(不了解的可以阅读我的<Android异步消息处理机 ...
- Java SPI机制实战详解及源码分析
背景介绍 提起SPI机制,可能很多人不太熟悉,它是由JDK直接提供的,全称为:Service Provider Interface.而在平时的使用过程中也很少遇到,但如果你阅读一些框架的源码时,会发现 ...
- select用法&原理详解(源码剖析)(转)
今天遇到了在select()前后fd_set的变化问题,查了好久终于找到一个有用的帖子了,很赞,很详细!!原文链接如下: select用法&原理详解(源码剖析) 我的问题是: 如下图示:在se ...
- 【转载】Android应用AsyncTask处理机制详解及源码分析
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重分享成果] 1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个 ...
- 详解ConCurrentHashMap源码(jdk1.8)
ConCurrentHashMap是一个支持高并发集合,常用的集合之一,在jdk1.8中ConCurrentHashMap的结构和操作和HashMap都很类似: 数据结构基于数组+链表/红黑树. ge ...
- 基于双向BiLstm神经网络的中文分词详解及源码
基于双向BiLstm神经网络的中文分词详解及源码 基于双向BiLstm神经网络的中文分词详解及源码 1 标注序列 2 训练网络 3 Viterbi算法求解最优路径 4 keras代码讲解 最后 源代码 ...
随机推荐
- Linux基础:Day02
Linux文件管理 创建 touch vim/vi echo重定向 touch 管理:atime mtime ctime touch 文件名 //如果文件不存在就创建文件 touch -a -t [ ...
- Linux bash篇(四 命令)
1.一次执行多个命令 ; eg: ls -al ; touch data.txt 2.根据情况执行命令 && || cmd1 && c ...
- 如何关闭php的所有错误提示
在调试PHP 应用程序时,应当知道两个配置变量.下面是这两个变量及其默认值:display_errors = Offerror_reporting = E_ALL E_ALL能从不良编码实践到无害提示 ...
- JAVA debug 调试demo
1.设置断点,在代码的行号后面鼠标左键即可2.想要看调用方法的执行流程,那么调用方法也要加断点. package day6_debug; /* * 1.设置断点,在代码的行号后面鼠标左键即可 * 2. ...
- 01-启动jmeter目录功能
1.bin :存储了jmeter的可执行程序,如启动脚本.配置程序 docs: api扩展文档存放 lib: lib\ext 存储了jmeter的整合的功能(如.jar文件程序,和第三方 ...
- 数据结构和算法(Golang实现)(11)常见数据结构-前言
常见数据结构及算法 数据结构主要用来组织数据,也作为数据的容器,载体. 各种各样的算法,都需要使用一定的数据结构来组织数据. 常见的典型数据结构有: 链表 栈和队列 树 图 上述可以延伸出各种各样的术 ...
- 表字段或表名出现Mysql关键字或保留字导致问题 Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have
MySQL 5.7使用的关键字和保留字 https://dev.mysql.com/doc/refman/5.7/en/keywords.html 当我们建表的时候如果使用了关键字或者保留字,则在执行 ...
- vue 本地调试跨域---带cookies(axios)
cookise跨域第二期之便捷优雅的本地调试(axios) 1.打开config/index.js,在proxyTable中添写如下代码: proxyTable: { '/agent': { //使用 ...
- AJ学IOS(07)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
AJ分享,必须精品 先看效果图: 学习代码 // // NYViewController.m // 05-UITextField事件_UIKit复习 // // Created by apple on ...
- python与excel的关系;铁打的python流水的excel
现在很多行业,都离不开用Excel: 做财务的,要用Excel做报表:做物流的,会用Excel来跟踪订单情况:做HR的,会用Excel算工资:做分析的,会用Excel计算数据做报表.不知道你有没有这样 ...