【SpringBoot3.0源码】启动流程源码解析 •下

news/2024/10/22 15:27:10/

文章目录

    • 初始化DefaultBootstrapContext
    • 开启Headless模式
    • 获取监听器并启动
    • 封装命令行参数
    • 准备环境
    • 打印Banner
    • 创建上下文容器
    • 预初始化上下文容器
    • 刷新Spring容器
    • 打印启动时间
    • 发布事件
    • 执行特定的run方法

上一篇《【SpringBoot3.0源码】启动流程源码解析 • 上》,主要讲解了new SpringApplication()设置了一些初始化器和监听器,接下来我们讲解下run方法的调用。

步入run方法:

在这里插入图片描述

public ConfigurableApplicationContext run(String... args) {// 记录时间long startTime = System.nanoTime();// 利用BootstrapRegistryInitializer初始化DefaultBootstrapContext对象DefaultBootstrapContext bootstrapContext = createBootstrapContext();ConfigurableApplicationContext context = null;// 开启了Headless模式configureHeadlessProperty();// 获取监听器SpringApplicationRunListeners listeners = getRunListeners(args);// 发布ApplicationStartingEvent事件listeners.starting(bootstrapContext, this.mainApplicationClass);try {// 根据命令行参数,实例化一个ApplicationArgumentsApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 准备环境ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);// 打印BannerBanner printedBanner = printBanner(environment);// 据webApplicationType创建不同的Spring上下文容器(有三种)context = createApplicationContext();context.setApplicationStartup(this.applicationStartup);// 预初始化spring上下文prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);// 刷新Spring容器refreshContext(context);afterRefresh(context, applicationArguments);// 打印启动时间Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);}// 发布ApplicationStartedEvent事件和AvailabilityChangeEvent事件listeners.started(context, timeTakenToStartup);// 获取Spring容器中的ApplicationRunner/CommandLineRunner类型的Bean,并执行run方法callRunners(context, applicationArguments);}catch (Throwable ex) {if (ex instanceof AbandonedRunException) {throw ex;}// 发布ApplicationFailedEvent事件handleRunFailure(context, ex, listeners);throw new IllegalStateException(ex);}try {if (context.isRunning()) {Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);// 发布ApplicationReadyEvent事件和AvailabilityChangeEvent事件listeners.ready(context, timeTakenToReady);}}catch (Throwable ex) {if (ex instanceof AbandonedRunException) {throw ex;}// 发布ApplicationFailedEvent事件handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;
}

初始化DefaultBootstrapContext

步入createBootstrapContext方法:

在这里插入图片描述

开启Headless模式

Headless模式是在缺少显示屏、键盘或者鼠标的系统配置。

步入configureHeadlessProperty方法:

private void configureHeadlessProperty() {System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
}

启用headless模式,需要使用setProperty方法去设置相应的系统属性。

System.setProperty(“java.awt.headless”,true)

如果想在一个相同的程序 中使用headless和传统环境,你可以使用下面的命令行来完成:

java -Djava.awt.headless=true

获取监听器并启动

SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);

获取SpringApplicationRunListeners,SpringBoot提供了一个EventPublishingRunListener,它实现了SpringApplicationRunListener接口
Spring会利用这个类,发布一个ApplicationContextInitializedEvent事件,可以通过定义ApplicationListener来监听这个事件。

步入getRunListeners方法:调用getSpringFactoriesInstances方法获取监听器,这个方法前面前面讲过,因为在前面已经put进m缓存中,所以这里可以根据参数获取value值。

最后返回一个SpringApplicationRunListener实例。

在这里插入图片描述
接下来回去调用:

listeners.starting(bootstrapContext, this.mainApplicationClass);

步入starting方法:

在这里插入图片描述
步入doWithListeners方法:

在这里插入图片描述
首先会调用listenerAction: (listener) -> listener.starting(bootstrapContext)

步入starting方法:

在这里插入图片描述

步入multicastInitialEvent方法:

在这里插入图片描述
步入refreshApplicationListeners方法:
这7个监听器是我们之前加载到的:
在这里插入图片描述
调用this.initialMulticaster::addApplicationListener方法:
显式删除代理的目标(如果已注册),以避免对同一侦听器进行双重调用。
add到applicationListeners的set集合中。
在这里插入图片描述
执行完返回:

在这里插入图片描述
步入multicastEvent方法:

在这里插入图片描述
步入invokeListener方法:

在这里插入图片描述
doInvokeListener

在这里插入图片描述
listener.onApplicationEvent(event);

在这里插入图片描述
onApplicationStartingEvent

在这里插入图片描述
beforeInitialize

在这里插入图片描述
最后回到这里:

在这里插入图片描述
在这里插入图片描述

封装命令行参数

在这里插入图片描述

命令行参数配置:
在这里插入图片描述

DefaultApplicationArguments构造方法:

public DefaultApplicationArguments(String... args) {Assert.notNull(args, "Args must not be null");this.source = new Source(args);this.args = args;
}

准备环境

读取环境变量(操作系统的环境变量/JVM的环境变量),读取配置文件信息(基于监听器,会利用EventPublishingRunListener发布一个ApplicationEnvironmentPreparedEvent事件,默认会有一个EnvironmentPostProcessorApplicationListener来处理这个事件,当然也可以通过自定义ApplicationListener来处理这个事件,当ApplicationListener接收到这个事件之后,就会解析application.properties、application.yml文件,并添加到Environment中。

在这里插入图片描述

步入prepareEnvironment方法:

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {// 根据不同的web类型创建不同实现的Environment对象,读取:java环境变量和系统环境变量ConfigurableEnvironment environment = getOrCreateEnvironment();// 将命令行参数读取环境变量中configureEnvironment(environment, applicationArguments.getSourceArgs());// 将@PropertieSource的配置信息 放在第一位,它的优先级是最低的ConfigurationPropertySources.attach(environment);// 发布了ApplicationEnvironmentPreparedEvent 的监听器 读取了全局配置文件listeners.environmentPrepared(bootstrapContext, environment);DefaultPropertiesPropertySource.moveToEnd(environment);Assert.state(!environment.containsProperty("spring.main.environment-prefix"),"Environment prefix cannot be set via properties.");// 将所有spring.main 开头的配置信息绑定到SpringApplication中bindToSpringApplication(environment);if (!this.isCustomEnvironment) {EnvironmentConverter environmentConverter = new EnvironmentConverter(getClassLoader());environment = environmentConverter.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());}// 更新PropertySources配置ConfigurationPropertySources.attach(environment);return environment;}

在这里插入图片描述

根据不同的web类型创建不同实现的Environment对象,读取java环境变量和系统环境变量

ConfigurableEnvironment environment = getOrCreateEnvironment();

在这里插入图片描述

// 将命令行参数读取环境变量中
configureEnvironment(environment, applicationArguments.getSourceArgs());

在这里插入图片描述

// 将@PropertieSource的配置信息 放在第一位,它的优先级是最低的
ConfigurationPropertySources.attach(environment);

在这里插入图片描述

// 发布了ApplicationEnvironmentPreparedEvent的监听器,读取了全局配置文件
listeners.environmentPrepared(bootstrapContext, environment);

最终调用onApplicationEnvironmentPreparedEvent方法:

在这里插入图片描述

步入postProcessEnvironment方法:

在这里插入图片描述
步入processAndApply方法:

在这里插入图片描述

步入applyToEnvironment方法:
在这里插入图片描述

// 将所有spring.main 开头的配置信息绑定到SpringApplication中
bindToSpringApplication(environment);

在这里插入图片描述

打印Banner

// 打印Banner
Banner printedBanner = printBanner(environment);

步入printBanner方法:

在这里插入图片描述
步入print方法:

在这里插入图片描述
步入getBanner方法:

在这里插入图片描述
步入getTextBanner方法:首先获取spring.banner.location的值,如果没有就默认在根路径下,输出banner.txt文件。

在这里插入图片描述

获取完banner后,会输出:

banner.printBanner(environment, sourceClass, out);

在这里插入图片描述

步入该方法,输出banner:

在这里插入图片描述

创建上下文容器

// 据webApplicationType创建不同的Spring上下文容器(有三种)
context = createApplicationContext();

在这里插入图片描述

预初始化上下文容器

prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);

步入prepareContext方法:

首先获取所有ApplicationContextInitializer, 循环调用initialize方法。

获取beanFactory。

对bean进行check,如果有重复的bean就会抛出异常。

将启动类注册到Spring容器中。

在这里插入图片描述

刷新Spring容器

这里前几章文章重点讲的,包括bean的加载、实例化、初始化、aop、事务、tomcat启动。

可以移步专栏查看:

《Java系核心技术》

打印启动时间

Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);}

发布事件

// 发布ApplicationStartedEvent事件和AvailabilityChangeEvent事件
listeners.started(context, timeTakenToStartup);

最终来到这个方法进行事件的发布:

在这里插入图片描述
最终进行发布:

在这里插入图片描述

执行特定的run方法

// 获取Spring容器中的ApplicationRunner/CommandLineRunner类型的Bean,并执行run方法
callRunners(context, applicationArguments);

ApplicationRunner和CommandLineRunner简介

在这里插入图片描述


后续就是发布ApplicationReadyEvent事件和AvailabilityChangeEvent事件,以及对异常的处理。


http://www.ppmy.cn/news/31968.html

相关文章

【HashMap】| 深度剥析Java SE 源码合集Ⅱ | 你会吗?

目录一. 🦁 HashMap介绍1.1 特点1.2 底层实现二. 🦁 结构以及对应方法分析2.1 结构组成2.1.1 成员变量2.1.2 存储元素的节点类型2.1.2.1 链表Node类2.1.2.2 树节点类2.1.2.3 继承关系2.2 方法实现2.2.1 HashMap的数组初始化2.2.2 计算hash值2.2.3 添加元…

华为OD机试真题2023(JAVA)

华为机试题库已由2022版换为2023版 华为机试有三道题目,第一道和第二道属于简单或中等题,分值为100分,第三道为中等或困难题,分值为200分。总分为400分,150分钟考试时间。之前通过为150分,现在好像分数提高…

【Java 语法篇】Java 六类运算符详解

算数运算符关系运算符逻辑运算符赋值运算符字符串连接运算符条件运算符在 Java 语言中,运算符有算数运算符、关系运算符、逻辑运算符、赋值运算符、字符串连接运算符、条件运算符。 算数运算符 算数运算符是我们最常用的运算符,用于完成基本的算术运算…

【手撕源码】vue2.x中keep-alive源码解析

🐱 个人主页:不叫猫先生 🙋‍♂️ 作者简介:前端领域新星创作者、阿里云专家博主,专注于前端各领域技术,共同学习共同进步,一起加油呀! 💫系列专栏:vue3从入门…

Pytorch实现EdgeCNN(基于PyTorch实现)

文章目录前言一、导入相关库二、加载Cora数据集三、定义EdgeCNN网络3.1 定义EdgeConv层3.1.1 特征拼接3.1.2 max聚合3.1.3 特征映射3.1.4 EdgeConv层3.2 定义EdgeCNN网络四、定义模型五、模型训练六、模型验证七、结果完整代码前言 大家好,我是阿光。 本专栏整理了…

【Windows】六种正确清理C盘的方法,解决你的红色烦恼

如何正确的清理C盘前言清理方法1. 利用Windows自己附带的磁盘清理工具2. 开启自动清理3. 通过“配置存储感知或立即运行”来清理4. 管理C盘中的程序5. 系统文件夹转移6. 将C盘现有内容转移到别的盘参考链接前言 Windows操作系统一般是安装在磁盘驱动器的C盘中,运行…

【Linux】冯.诺依曼体系结构与操作系统

环境:centos7.6,腾讯云服务器Linux文章都放在了专栏:【Linux】欢迎支持订阅🌹冯.诺依曼体系结构什么是冯诺依曼体系结构?我们如今的计算机比如笔记本,或者是服务器,基本上都遵循冯诺依曼体系结构…

英雄算法学习路线

文章目录零、自我介绍一、关于拜师二、关于编程语言三、算法学习路线1、算法集训1)九日集训2)每月算法集训2、算法专栏3、算法总包四、英雄算法联盟1、英雄算法联盟是什么?2、如何加入英雄算法联盟?3、为何会有英雄算法联盟&#…