SpringBoot 启动配置原理
几个重要的事件回调机制
- ApplicationContextInitializer
- SpringApplicationRunListener
- ApplicationRunner
- CommandLineRunner
启动流程:
- 创建SpringApplication对象
/**
* 在springboot 2以前:
* 调用initialize(source);
* 在springboot 2以后,跳过了initialize()方法,直接调用run()方法
*/ private void initialize(Object[] source){
// 保存主配置类
if(sources != null && source.length > 0){
this.sources.addAll(Arrays.asList(sources));
}
// 判断当前是否是一个web应用
this.webEnvironment = deduceWebEnvironment();
// 从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
setInitializes((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 从类路径下找到META-INF/spring.factories配置的所有ApplicationListener;然后保存起来
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 从多个配置类中找到有main方法的主配置类
this.mainApplicationClass = deduceMainApplicationClass();
} - 运行run方法
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty(); // 获取SpringApplicationRunListener;从类路径下META-INF/spring.factories找到
SpringApplicationRunListeners listeners = getRunListeners(args);
// 回调所有的SpringApplicationRunListener.starting()方法
listeners.starting();
try {
// 封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 准备环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// 创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment); // 创建ApplicationContext;决定创建web的ioc还是普通的ioc
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
// 准备上下文环境;将environment保存到ioc中
// applyInitializers();回调之前保存的所有的SpringApplicationRunListener的initializers方法 prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 回调所有的SpringApplicationRunListener的contextPrepared();
// 刷新容器;ioc容器初始化完成
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
} try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
SpringBoot 启动配置原理的更多相关文章
- springboot启动配置原理之二(运行run方法)
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); s ...
- springboot 启动配置原理【转】【补】
创建应用 几个重要的事件回调机制 , 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunL ...
- springboot启动配置原理之一(创建SpringApplication对象)
几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...
- springboot启动配置原理之三(事件监听机制)
ApplicationContextInitializer public class HelloApplicationContextInitializer implements Application ...
- SpringBoot启动过程原理
最近这两年springboot突然火起来了,那么我们就来看看springboot的运行原理. 一.springboot的三种启动方式: 1.运行带有main方法的2.通过命令 Java -jar命令3 ...
- 这样讲 SpringBoot 自动配置原理,你应该能明白了吧
https://juejin.im/post/5ce5effb6fb9a07f0b039a14 前言 小伙伴们是否想起曾经被 SSM 整合支配的恐惧?相信很多小伙伴都是有过这样的经历的,一大堆配置问题 ...
- 【SpringBoot1.x】SpringBoot1.x 启动配置原理 和 自定义starter
SpringBoot1.x 启动配置原理 和 自定义starter 启动配置原理 本节源码 启动过程主要为: new SpringApplication(sources) 创建 SpringAppli ...
- springboot自动配置原理以及手动实现配置类
springboot自动配置原理以及手动实现配置类 1.原理 spring有一个思想是"约定大于配置". 配置类自动配置可以帮助开发人员更加专注于业务逻辑开发,springboot ...
- SpringBoot自动配置原理
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾前面Spring的文章(以学习的顺序排好): S ...
随机推荐
- 小程序util.js的使用
我们通过开发者工具快速创建了一个 QuickStart 项目.你可以留意到这个项目里边生成了一个utils/util.js这里写图片描述 可以将一些公共的代码抽离成为一个单独的 js (utils.j ...
- centOS 6.8下使用Gparted进行分区扩容
centOS 6.8下使用Gparted进行分区扩容 机器环境:windows上运行的VMware虚拟机,系统为centOS 6.8. 由于前期分区分配空间过小,无法满足后续的数据存储预期,所 ...
- Wilson's theorem在RSA题中运用
引言 最近一段时间在再练习数论相关的密码学题目,自己之前对于数论掌握不是很熟练,借此机会先对数论基本的四大定理进行练习 这次的练习时基于Wilson's theorem(威尔逊定理)在RSA题目中的练 ...
- Vim的三款实用插件
Vim 是 Linux 下的常用文本编辑器,但也经常被称为是一个上古神器,因为它对于初学者而言相当不友好,也不好入门. 但是,对于高手而言,他们不仅将 Vim 玩得很溜,而且还将它当作代码开发的主要工 ...
- Binlog2sql+CentOS7 离线安装
Binlog2sql+CentOS7 离线安装 目录 Binlog2sql+CentOS7 离线安装 1. 环境 2. 下载 3.1 Pip 安装 3.2 PyMySQL/mysql-replicat ...
- java中的垃圾处理机制
1.何为垃圾在Java中,如果对象实体没有引用指向的话,存储该实体的内存便成为垃圾.JVM会有一个系统线程专门负责回收垃圾.垃圾同时包括分配对象内存间的碎片块 2.垃圾处理包含的算法 Java语言规范 ...
- 概念辨析-Description Language还是Description Library?
https://mp.weixin.qq.com/s/p7eyD6GkniFGHrnr8t2SZQ 概念辨析-Description Language还是Description Library? ...
- 蒲公英 · JELLY技术周刊 Vol.09 StackOverflow - 2020 开发者年度报告
登高远眺 沧海拾遗,积跬步以至千里 基础技术 StackOverFlow 2020 年开发者报告 技术问答社区 StackOverFlow 的年度报告,本次报告统计了来自于全球各地共 65000 名开 ...
- 一:HTTP协议解析
一:HTTP协议解析 1.HTTP协议即超文本传输协议,是一种详细规定了浏览器和万维网服务器之间互相通信的规则,他是万维网交换信息的基础,它允许将HTML(超文本标记语言)文档从web服务器传送到we ...
- Java实现 LeetCode 38 外观数列
38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...