Spring Boot————Spring Boot启动流程分析

一、引言

Spring Boot 的启动虽然仅仅是执行了一个main方法,但实际上,运行流程还是比较复杂的,其中包含几个非常重要的事件回调机制。在实际生产开发中,有时候也会利用这些启动流程中的回调机制,做一些项目初始化的工作,比如内存初始化等。所以,学习Spring Boot启动流程非常重要。

二、启动流程概述

SpringApplication.run(Object, String...)方法的执行中包括以下一些关键步骤:

1、准备环境: 

  1. 执行ApplicationContextInitializer.initialize()
  2. 监听器SpringApplicationRunListener回调contextPrepared()
  3. 加载主配置类(启动类)定义信息
  4. 监听器SpringApplicationRunListener回调contextLoaded()

2、刷新启动IOC容器: 

  1. 扫描加载所有容器中的组件
  2. 从META-INF/spring.factories中获取所有的EnableAutoConfiguration组件

3、回调容器中所有的 ApplicationRunner 、CommandLineRunner 的 run() 方法

4、监听器 SpringApplicationRunListener 回调 finished()方法

三、详细流程剖析

Spring Boot的启动方法调用流程为两步:1、创建SpringApplication对象;2、执行run()方法

这句代码是一个中间的调用过程,接下来,我们将深度讲解创建SpringApplication对象和执行run()方法具体都做了哪些工作。 

1、创建SpringApplication对象

通过SpringApplication的构造器,调用initialize()方法,对SpringApplication中的一些属性初始化默认值,同时,从META-INF/spring.factories找到所有ApplicationContextInitializer和ApplicationListener保存起来。

@SuppressWarnings({ "unchecked", "rawtypes" })
private void initialize(Object[] sources) {// 保存主配置类if (sources != null && sources.length > 0) {this.sources.addAll(Arrays.asList(sources));}// 判断当前应用是否为一个WEB应用this.webEnvironment = deduceWebEnvironment();// 从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer,然后保存起来setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));// 从类路径下找到META-INF/spring.factories配置的所有ApplicationListenersetListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));// 从多个主配置类中找到有main方法的主配置类this.mainApplicationClass = deduceMainApplicationClass();
}

2、运行run(String...)方法

主要做了两件事:

  • 回调:ApplicationContextInitializer和SpringApplicationRunListener;
  • 回调:ApplicationRunner和CommandLineRunner。

详细流程注释如下:

/*** Run the Spring application, creating and refreshing a new* {@link ApplicationContext}.* * @param args*            the application arguments (usually passed from a Java main*            method)* @return a running {@link ApplicationContext}*/
public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;FailureAnalyzers analyzers = null;configureHeadlessProperty();// 获取SpringApplicationRunListeners,从类路径下META-INF/spring-factoriesSpringApplicationRunListeners listeners = getRunListeners(args);// 循环所有的listener,回调starting()方法listeners.starting();try {// 封装命令行参数ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);/** 准备环境:* 创建环境完成后回调SpringApplicationRunListener.environmentPrepared()方法,表示* 环境准备完成。*/ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);// 打印控制台的Spring 字符画Banner printedBanner = printBanner(environment);// 创建IOC容器:ApplicationContext;决定创建web IOC还是普通的IOC容器。context = createApplicationContext();// 创建错误分析对象analyzers = new FailureAnalyzers(context);/** 准备上下文环境,将environment保存到IOC容器中; 而且applyInitializers(),* 回调之前保存的所有的applicationContextInitializer的initialize()方法;* 回调所有的SpringApplicationRunListener的contextPrepareded();最后回调* 所有的SpringApplicationRunListener的contextLoaded()方法*/prepareContext(context, environment, listeners, applicationArguments, printedBanner);/** 刷新容器:IOC容器的初始化(扫描所有的配置类、@Bean等,加载并创建IOC容器中所有的组件。如果是web应用,* 还会创建嵌入式的tomcat),当执行完refreshContext()后,IOC容器即创建完毕*/refreshContext(context);/** 从IOC容器中获取所有的ApplicationRunner和CommandLineRunner,* 然后先回调ApplicationRunner 再回调 CommandLineRunner*/afterRefresh(context, applicationArguments);// 所有的SpringApplicationRunListener回调finished()方法listeners.finished(context, null);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}// 整个Spring Boot应用启动完成后,返回IOC容器return context;} catch (Throwable ex) {handleRunFailure(context, listeners, analyzers, ex);throw new IllegalStateException(ex);}
}

四、总结

Spring Boot 的启动流程是:

1、准备环境

2、刷新启动IOC容器: 

3、回调容器中所有的 ApplicationRunner 、CommandLineRunner 的 run() 方法

4、监听器 SpringApplicationRunListener 回调 finished()方法

在“详细启动流程” 中,已经将run()方法中实际执行流程用注释的方式标记出来了,里面的方法都通过调用的方式完成了一些特定的功能,最主要的是把握他们的执行顺序和完成内容,可以通过debug的方式,并观察控制台输出和参数内容来进行追踪学习。

综上,就是关于Spring Boot启动的完整流程,欢迎文末留言。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558864.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode算法入门- Longest Valid Parentheses -day12

LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. 题目描述: 题目的意思是给定一个括号序列的字符串&#xff…

Spring Boot————应用启动时的监听机制测试

引言 本文承接前面的《Spring Boot————Spring Boot启动流程分析》,主要测试一下ApplicationContextInitializer、SpringApplicationRunListener、ApplicationRunner、CommandLineRunner这四个接口实现之下的组件是何时在Spring Boot项目启动时创建并执行相关方…

LeetCode算法入门- Longest Common Prefix -day13

LeetCode算法入门- Longest Common Prefix -day13 题目描述: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Example 1: Input: [“flower”,“flow”…

HTMLCSS————块元素与内联元素

一、块元素与div标签 div是一个块元素&#xff0c;块元素会独占一行&#xff0c;无论它的内容有多少&#xff0c;都会独占一整行。 类似的块元素还有&#xff1a;<p>、<h1>、<h2>、<h3> 然而&#xff0c;<div>标签没有任何语义&#xff0c;就…

LeetCode算法入门- Compare Version Numbers -day14

LeetCode算法入门- Compare Version Numbers -day14 题目描述&#xff1a; Compare two version numbers version1 and version2. If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings a…

HTMLCSS————CSS常用选择器及优先级

选择器优先级 内联样式&#xff08;1000&#xff09;> id选择器&#xff08;100&#xff09;> 类和伪类选择器&#xff08;10&#xff09; > 元素选择器&#xff08;1&#xff09;>通配 * 选择器&#xff08;0&#xff09;> 继承的样式 一、元素选择器 作用&…

LeetCode算法入门- Merge Two Sorted Lists -day15

LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述&#xff1a; Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3-&g…

2018年度总结

2018年&#xff0c;已经成为过去式&#xff0c;这360多天依旧过的很快&#xff0c;快到当我手扶键盘回想这一年发生的点点滴滴时&#xff0c;都没有任何感慨。可能我天生是个无感之人&#xff0c;或许&#xff0c;这一年的时光&#xff0c;无数的事故、故事已经让我变得不那么感…

LeetCode算法入门- Generate Parentheses -day16

LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n 3, a solution set is: [ “((()))”, “(()())”, “(())()”, “()(())”, …

2019技术学习规划

引言 前段时间总结了一下2018年的大事小情&#xff08;《2018年度总结》&#xff09;&#xff0c;整体来说还是正能量满满&#xff0c;阅读量涨得也是蛮快的。今天&#xff0c;抽出点时间思考了一下未来一年的规划。那作为技术人才&#xff0c;规划也自然都是技术相关的&#…

LeetCode算法入门- Remove Nth Node From End of List -day17

LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释&#xff1a; Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n 2. After removing the seco…

Spring Boot————默认缓存应用及原理

引言 应用程序的数据除了可以放在配置文件中、数据库中以外&#xff0c;还会有相当一部分存储在计算机的内存中&#xff0c;这部分数据访问速度要快于数据库的访问&#xff0c;因此通常在做提升数据访问速度时&#xff0c;会将需要提升访问速度的数据放入到内存中&#xff0c;…

LeetCode算法入门- Multiply Strings -day18

LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 “2”, num2 “3” Output: “6” Exampl…

Linux下查看版本及系统信息

一、查看Linux发行版本 [rootlocalhost ~]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m[rootlocalhost ~]# cat /etc/redhat-release CentOS release 6.8 (Final)二、查看Linux内核信息 [rootlocalhost ~]# uname -a Linux localhost.localdomain 2.6.32…

LeetCode算法入门- Search Insert Position -day19

LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array.…

Linux——VMware虚拟机安装CentOS步骤

一、下载CentOS.iso镜像 最地道的下载方式就是通过官网&#xff0c;大多数的网上连接会直接抛出网易、华为的镜像连接&#xff0c;实际上这些连接都可以在官网找到&#xff1a; 官网地址&#xff08;可直接百度搜索CentOS&#xff09;&#xff1a;https://www.centos.org/ 1…

LeetCode算法入门- Remove Element -day20

LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array i…

Spring Boot——Redis安装配置与应用整合

引言 Spring Boot默认以ConcurrentHashMap作为缓存容器&#xff0c;但默认的缓存容器在简单的场景使用还是可以的&#xff0c;而作为NoSQL的代表&#xff0c;Redis可以做内存数据库、消息中间件都是不错的&#xff0c;而且有RedisDesktopManager作为可视化管理工具&#xff0c…

利用Aria2高速下载网盘文件

利用Aria2高速下载网盘文件 方法步骤&#xff1a; 下载文件 解压arial2&#xff0c;运行aria2启动.VBS添加插件&#xff0c;解压BaiduExporter-master.zip在Google浏览器扩展程序中chrome://extensions加载已经解压的扩展程序 选择BaiduExporter进行添加即可&#xff0c;打开…

MySQL——JSON_REPLACE()函数修改JSON属性值

引言 由于对mysql的函数并不了解&#xff0c;之前遇到了一个场景&#xff1a; mysql表中有一个字段res_content 是一个由longtext类型&#xff08;可以理解为一个更长的varchar&#xff09;保存的巨大的JSON对象&#xff0c;但是&#xff0c;由于录入的疏忽&#xff0c;导致这…