从源码角度解析 Springboot 2.6.2 的启动过程
1. 概述
老话说的好:把简单的事情重复做,做到极致,你就成功了。
言归正传,Springboot的启动过程,一直都是面试的高频点,今天我们用当前最新的 Springboot 2.6.2 来聊一聊 Springboot 的启动过程。
2. 工程搭建
2.1 maven 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2 application.yml 配置文件
server:
port: 30000 spring:
application:
name: my-springboot
2.3 启动类代码
@SpringBootApplication
public class MySpringbootApplication { public static void main(String[] args) {
SpringApplication.run(MySpringbootApplication.class, args);
}
}
3. Springboot 的启动主流程
3.1 入口
入口当然就是我们 Springboot 启动类中 main 方法里的这段代码,SpringApplication.run 方法
3.2 初始化 SpringApplication 实例
3.2.1 方法总览
我们进入 SpringApplication.run 这个静态方法
这里调用了 另一个重载的 run 方法,再进
此处会 new 一个 SpringApplication 对象,然后调用这个对象的 run 方法
我们来看一下 SpringApplication 对象实例化时做的事情,这个构造方法调用了另一个重载的构造方法,我们进去看下
3.2.2
this.resourceLoader = resourceLoader; // resourceLoader 属性注入了 null
3.2.3
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); // 将启动类从数组重新封装成了 Set,注入到 primarySources 属性
3.2.4
this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 得到 web应用的类型,这里是 SERVLET
webApplicationType 有三种类型,REACTIVE、SERVLET、NONE
引入 spring-boot-starter-web 包,就是 SERVLET
引入 spring-boot-starter-webflux 包,是 REACTIVE
都没有就是 NONE
3.2.5
this.bootstrapRegistryInitializers = new ArrayList<>(getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
从 META-INF/spring.factories 文件中得到 key 为 org.springframework.boot.BootstrapRegistryInitializer 的全类名集合,进行实例化,然后注入 bootstrapRegistryInitializers 属性
这里大家先记下 getSpringFactoriesInstances 方法,等下详细介绍
3.2.6
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
这一行代码,只是封装了一下,仍然还是调用 getSpringFactoriesInstances 方法,从 META-INF/spring.factories 文件中得到 key 为
org.springframework.context.ApplicationContextInitializer 的全类名集合,进行实例化,然后注入 initializers(初始化器集合) 属性。
3.2.7
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); // 同理,得到监听器实例的集合,并注入
3.2.8
this.mainApplicationClass = deduceMainApplicationClass(); // 获取当前运行的 main 方法所在的类,也就是咱们的主类
3.3 执行 run 方法
3.3.1 方法总览
我们回到这个方法体,进入 run 方法
方法有点长。。。,没关系,我们捡重点看看
3.3.2
long startTime = System.nanoTime(); // 记录一个开始时间戳
3.3.3
DefaultBootstrapContext bootstrapContext = createBootstrapContext(); // 添加了一个默认的 Bootstrap 上下文
从代码看,就是 new 了一个 DefaultBootstrapContext 实例,然后遍历初始化了 bootstrapRegistryInitializers 中的所有初始化器
还记得 bootstrapRegistryInitializers 属性吗,3.2.5 章节中,实例化 SpringApplication 时通过 getSpringFactoriesInstances 方法获得并注入的。
3.3.4
configureHeadlessProperty(); // 配置Headless属性
3.3.5
SpringApplicationRunListeners listeners = getRunListeners(args); // 获得 RunListener 集合类
这里我们又看到了熟悉的 getSpringFactoriesInstances,这次的 key 是 org.springframework.boot.SpringApplicationRunListener
这里会得到 EventPublishingRunListener 对象
3.3.6
listeners.starting(bootstrapContext, this.mainApplicationClass); // 循环启动这些监听器
从代码看,会调用监听器的 starting 方法,我们看一下 EventPublishingRunListener 对象的 starting 方法
从代码看,在 EventPublishingRunListener 对象的 starting 方法中,做了一个广播,得到应用监听器后,循环调用监听器的 onApplicationEvent 方法
3.3.7
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 封装参数
3.3.8
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); // 创建并配置环境
首先会创建环境,因为 webApplicationType 是 SERVLET,因此会创建 ApplicationServletEnvironment 对象
listeners.environmentPrepared(bootstrapContext, environment);
重点是这句
listeners.environmentPrepared 方法会执行 EventPublishingRunListener 对象的 environmentPrepared 方法
来到 EventPublishingRunListener 对象的方法,同样是一个广播,广播给合适的监听器,然后调用监听器的 onApplicationEvent 方法
其中在 EnvironmentPostProcessorApplicationListener 监听器中,会执行拿到所有系统的配置,包括我们在 application.yml 文件中配置的内容。
我们来看一下 EnvironmentPostProcessorApplicationListener 这个类
在 EnvironmentPostProcessorApplicationListener 中,会得到环境的处理器,然后循环执行他们
这里可以得到 7 个处理器,其中 ConfigDataEnvironmentPostProcessor 就是加载配置文件得到配置的,我们来看一下这个类的 postProcessEnvironment 方法
在方法中,执行 processAndApply() 方法,最终拿到配置
当 listeners.environmentPrepared(bootstrapContext, environment); 最终执行完,我们从 environment 对象中就可以找到我们在 yml 文件中配置的 端口 和 应用名称
3.3.9
Banner printedBanner = printBanner(environment); // 打印 Banner
3.3.10
context = createApplicationContext(); // 实例化上下文对象
因为类型是 SERVLET,所以实例化的是 AnnotationConfigServletWebServerApplicationContext 对象
3.3.11
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); // 准备上下文
3.3.12
refreshContext(context); // 刷新上下文
主要逻辑在 AbstractApplicationContext 对象的 refresh 方法中
3.3.13
afterRefresh(context, applicationArguments); // 空方法
3.3.14
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime); // 计算耗时
3.3.15
listeners.started(context, timeTakenToStartup); // 监听器执行 started 方法,表示启动成功
3.3.16
callRunners(context, applicationArguments); // 回调所有的ApplicationRunner和CommandLineRunner
3.3.17
listeners.ready(context, timeTakenToReady); // 监听器执行 ready 方法
4. 流程总结
1)实例化 SpringApplication 对象
2)得到 初始化器 和 监听器
3)调用 run 方法
4)记录开始时间
5)得到 runListeners
6)runListeners 执行 starting
7)准备环境
8)打印 banner
9)实例化上下文对象
10)准备上下文,执行之前得到的初始化器的初始化方法,load主bean
11)刷新上下文,在其中加载 autoConfiguration,并启动 Tomcat
12)计算耗时
13)打印耗时
14)通知监听器启动完成
15)通知监听器 ready
5. getSpringFactoriesInstances 方法详解
这里面比较关键的逻辑是 得到类的全类名集合 和 实例化类
从这些代码我们可以得知,会从 META-INF/spring.factories 文件中找到 key 匹配的类,并把类的全路径集合得到
例如实例化 SpringApplication 对象时,获得 初始化器 和 监听器
之后通过全类名,使用反射技术,实例化类,最终得到想要的集合
6. 综述
今天聊了一下 Springboot 2.6.2 的启动过程,希望可以对大家的工作有所帮助
欢迎帮忙点赞、评论、转发、加关注 :)
关注追风人聊Java,每天更新Java干货。
7. 个人公众号
追风人聊Java,欢迎大家关注
从源码角度解析 Springboot 2.6.2 的启动过程的更多相关文章
- 从源码角度解析Netty的React模式是如何工作的
Netty 支持多种实现方式,比如nio,epoll 等,本文以nio的实现方式进行讲解. 1.EventLoop : 事件循环看,简单来说就是一个死循环监听事件,如果事件来了,处理掉.通常做法就是开 ...
- spring源码学习之:spring容器的applicationContext启动过程
Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...
- SpringBoot源码学习1——SpringBoot自动装配源码解析+Spring如何处理配置类的
系列文章目录和关于我 一丶什么是SpringBoot自动装配 SpringBoot通过SPI的机制,在我们程序员引入一些starter之后,扫描外部引用 jar 包中的META-INF/spring. ...
- 消息队列高手课,带你从源码角度全面解析MQ的设计与实现
消息队列中间件的使用并不复杂,但如果你对消息队列不熟悉,很难构建出健壮.稳定并且高性能的企业级系统,你会面临很多实际问题: 如何选择最适合系统的消息队列产品? 如何保证消息不重复.不丢失? 如果你掌握 ...
- 从template到DOM(Vue.js源码角度看内部运行机制)
写在前面 这篇文章算是对最近写的一系列Vue.js源码的文章(https://github.com/answershuto/learnVue)的总结吧,在阅读源码的过程中也确实受益匪浅,希望自己的这些 ...
- mybatis 3.x源码深度解析与最佳实践(最完整原创)
mybatis 3.x源码深度解析与最佳实践 1 环境准备 1.1 mybatis介绍以及框架源码的学习目标 1.2 本系列源码解析的方式 1.3 环境搭建 1.4 从Hello World开始 2 ...
- Android布局性能优化—从源码角度看ViewStub延迟加载技术
在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码 ...
- Universal-Image-Loader源码解解析---display过程 + 获取bitmap过程
Universal-Image-Loader在github上的地址:https://github.com/nostra13/Android-Universal-Image-Loader 它的基本使用请 ...
- SpringBoot源码分析之SpringBoot的启动过程
SpringBoot源码分析之SpringBoot的启动过程 发表于 2017-04-30 | 分类于 springboot | 0 Comments | 阅读次数 SpringB ...
随机推荐
- CF1156F Card Bag
题目传送门. 题意简述:有 \(n\) 张卡牌,每张卡牌有数字 \(a_1,a_2,\cdots,a_n\).现在随机抽取卡牌,不放回,设本次抽到的卡牌为 \(x\),上次抽到的卡牌为 \(y\),若 ...
- NECAT组装ONT long reads
NECAT 可用于ONT数据的纠错,组装,如果想对ONT long reads进行call SV,也可以使用necatsv. githup网址:https://github.com/xiaochuan ...
- 【GS模型】全基因组选择之rrBLUP
目录 1. 理论 2. 实操 2.1 rrBLUP包简介 2.2 实操 3. 补充说明 关于模型 关于交叉验证 参考资料 1. 理论 rrBLUP是基因组选择最常用的模型之一,也是间接法模型的代表.回 ...
- 【swift】复制后,为Xcode工程项目重新修改名称
感谢,参考了另一篇博客:https://www.jianshu.com/p/abf10c9609ef 我做了一些修改,和自己遇到的情况 我用的是繁体的mac,所以下面图片内,鼠标右键点出来的文字(丢到 ...
- [转]C++中const的使用
原文链接:http://www.cnblogs.com/xudong-bupt/p/3509567.html 平时在写C++代码的时候不怎么注重const的使用,长久以来就把const的用法忘记了 写 ...
- mybatis缓存+aop出现的问题
在对某些特殊数据进行转换时,getOne方法后执行fieleInfoHandle进行转换,如果直接使用fixedTableData进行操作,没有后续的二次调用这样是没问题的,但是在后面当执行完upda ...
- Insert into select语句引发的生产事故
前言 Insert into select请慎用.这天xxx接到一个需求,需要将表A的数据迁移到表B中去做一个备份.本想通过程序先查询查出来然后批量插入.但xxx觉得这样有点慢,需要耗费大量的网络 ...
- Linux学习 - 权限管理命令
一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod [(ugoa) ...
- Spring Boot中使用模板引擎Thymeleaf
一.Thymeleaf简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用Java语言开发.Java中常见的模板引擎有Velocity.Freemaker.Th ...
- git push大文件失败(write error: Broken pipe)完美解决
问题 在使用git push推送大文件(超过了100MB)到GitHub远程仓库时提示异常,异常信息如下: fatal: sha1 file '<stdout>' write error: ...