Spring Boot 2.x 启动全过程源码分析(全)

转载自    Spring Boot 2.x 启动全过程源码分析(全)

上篇《Spring Boot 2.x 启动全过程源码分析(一)入口类剖析》我们分析了 Spring Boot 入口类 SpringApplication 的源码,并知道了其构造原理,这篇我们继续往下面分析其核心 run 方法。

SpringApplication 实例 run 方法运行过程

上面分析了 SpringApplication 实例对象构造方法初始化过程,下面继续来看下这个 SpringApplication 对象的 run 方法的源码和运行流程。

public ConfigurableApplicationContext run(String... args) {// 1、创建并启动计时监控类StopWatch stopWatch = new StopWatch();stopWatch.start();// 2、初始化应用上下文和异常报告集合ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();// 3、设置系统属性 `java.awt.headless` 的值,默认值为:trueconfigureHeadlessProperty();// 4、创建所有 Spring 运行监听器并发布应用启动事件SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting();try {// 5、初始化默认应用参数类ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 6、根据运行监听器和应用参数来准备 Spring 环境ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);configureIgnoreBeanInfo(environment);// 7、创建 Banner 打印类Banner printedBanner = printBanner(environment);// 8、创建应用上下文context = createApplicationContext();// 9、准备异常报告器exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);// 10、准备应用上下文prepareContext(context, environment, listeners, applicationArguments,printedBanner);// 11、刷新应用上下文refreshContext(context);// 12、应用上下文刷新后置处理afterRefresh(context, applicationArguments);// 13、停止计时监控类stopWatch.stop();// 14、输出日志记录执行主类名、时间信息if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}// 15、发布应用上下文启动完成事件listeners.started(context);// 16、执行所有 Runner 运行器callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, listeners);throw new IllegalStateException(ex);}try {// 17、发布应用上下文就绪事件listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, null);throw new IllegalStateException(ex);}// 18、返回应用上下文return context;
}

所以,我们可以按以下几步来分解 run 方法的启动过程。

1、创建并启动计时监控类

StopWatch stopWatch = new StopWatch();
stopWatch.start();

来看下这个计时监控类 StopWatch 的相关源码:

public void start() throws IllegalStateException {start("");
}public void start(String taskName) throws IllegalStateException {if (this.currentTaskName != null) {throw new IllegalStateException("Can't start StopWatch: it's already running");}this.currentTaskName = taskName;this.startTimeMillis = System.currentTimeMillis();
}

首先记录了当前任务的名称,默认为空字符串,然后记录当前 Spring Boot 应用启动的开始时间。

2、初始化应用上下文和异常报告集合

ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();

3、设置系统属性 `java.awt.headless` 的值

configureHeadlessProperty();

设置该默认值为:true,Java.awt.headless = true 有什么作用?

对于一个 Java 服务器来说经常要处理一些图形元素,例如地图的创建或者图形和图表等。这些API基本上总是需要运行一个X-server以便能使用AWT(Abstract Window Toolkit,抽象窗口工具集)。然而运行一个不必要的 X-server 并不是一种好的管理方式。有时你甚至不能运行 X-server,因此最好的方案是运行 headless 服务器,来进行简单的图像处理。

参考:www.cnblogs.com/princessd8251/p/4000016.html

4、创建所有 Spring 运行监听器并发布应用启动事件

SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();

来看下创建 Spring 运行监听器相关的源码:

private SpringApplicationRunListeners getRunListeners(String[] args) {Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}SpringApplicationRunListeners(Log log,Collection<? extends SpringApplicationRunListener> listeners) {this.log = log;this.listeners = new ArrayList<>(listeners);
}

创建逻辑和之前实例化初始化器和监听器的一样,一样调用的是 getSpringFactoriesInstances 方法来获取配置的监听器名称并实例化所有的类。

SpringApplicationRunListener 所有监听器配置在 spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories 这个配置文件里面。

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

5、初始化默认应用参数类

ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

6、根据运行监听器和应用参数来准备 Spring 环境

ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
configureIgnoreBeanInfo(environment);

下面我们主要来看下准备环境的 prepareEnvironment 源码:

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments) {// 6.1) 获取(或者创建)应用环境ConfigurableEnvironment environment = getOrCreateEnvironment();// 6.2) 配置应用环境configureEnvironment(environment, applicationArguments.getSourceArgs());listeners.environmentPrepared(environment);bindToSpringApplication(environment);if (this.webApplicationType == WebApplicationType.NONE) {environment = new EnvironmentConverter(getClassLoader()).convertToStandardEnvironmentIfNecessary(environment);}ConfigurationPropertySources.attach(environment);return environment;
}

6.1) 获取(或者创建)应用环境

private ConfigurableEnvironment getOrCreateEnvironment() {if (this.environment != null) {return this.environment;}if (this.webApplicationType == WebApplicationType.SERVLET) {return new StandardServletEnvironment();}return new StandardEnvironment();
}

这里分为标准 Servlet 环境和标准环境。

6.2) 配置应用环境

protected void configureEnvironment(ConfigurableEnvironment environment,String[] args) {configurePropertySources(environment, args);configureProfiles(environment, args);
}

这里分为以下两步来配置应用环境。

  • 配置 property sources

  • 配置 Profiles

这里主要处理所有 property sources 配置和 profiles 配置。

7、创建 Banner 打印类

Banner printedBanner = printBanner(environment);

这是用来打印 Banner 的处理类,这个没什么好说的。

8、创建应用上下文

context = createApplicationContext();

来看下 createApplicationContext() 方法的源码:

protected ConfigurableApplicationContext createApplicationContext() {Class<?> contextClass = this.applicationContextClass;if (contextClass == null) {try {switch (this.webApplicationType) {case SERVLET:contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);break;case REACTIVE:contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);break;default:contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);}}catch (ClassNotFoundException ex) {throw new IllegalStateException("Unable create a default ApplicationContext, "+ "please specify an ApplicationContextClass",ex);}}return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

其实就是根据不同的应用类型初始化不同的上下文应用类。

9、准备异常报告器

exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);

逻辑和之前实例化初始化器和监听器的一样,一样调用的是 getSpringFactoriesInstances 方法来获取配置的异常类名称并实例化所有的异常处理类。

该异常报告处理类配置在 spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories 这个配置文件里面。

# Error Reporters
org.springframework.boot.SpringBootExceptionReporter=\
org.springframework.boot.diagnostics.FailureAnalyzers

10、准备应用上下文

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

来看下 prepareContext() 方法的源码:

private void prepareContext(ConfigurableApplicationContext context,ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments, Banner printedBanner) {// 10.1)绑定环境到上下文context.setEnvironment(environment);// 10.2)配置上下文的 bean 生成器及资源加载器postProcessApplicationContext(context);// 10.3)为上下文应用所有初始化器applyInitializers(context);// 10.4)触发所有 SpringApplicationRunListener 监听器的 contextPrepared 事件方法listeners.contextPrepared(context);// 10.5)记录启动日志if (this.logStartupInfo) {logStartupInfo(context.getParent() == null);logStartupProfileInfo(context);}// 10.6)注册两个特殊的单例beancontext.getBeanFactory().registerSingleton("springApplicationArguments",applicationArguments);if (printedBanner != null) {context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);}// 10.7)加载所有资源Set<Object> sources = getAllSources();Assert.notEmpty(sources, "Sources must not be empty");load(context, sources.toArray(new Object[0]));// 10.8)触发所有 SpringApplicationRunListener 监听器的 contextLoaded 事件方法listeners.contextLoaded(context);
}

11、刷新应用上下文

refreshContext(context);

这个主要是刷新 Spring 的应用上下文,源码如下,不详细说明。

private void refreshContext(ConfigurableApplicationContext context) {refresh(context);if (this.registerShutdownHook) {try {context.registerShutdownHook();}catch (AccessControlException ex) {// Not allowed in some environments.}}
}

12、应用上下文刷新后置处理

afterRefresh(context, applicationArguments);

看了下这个方法的源码是空的,目前可以做一些自定义的后置处理操作。

/*** Called after the context has been refreshed.* @param context the application context* @param args the application arguments*/
protected void afterRefresh(ConfigurableApplicationContext context,ApplicationArguments args) {
}

13、停止计时监控类

stopWatch.stop();
public void stop() throws IllegalStateException {if (this.currentTaskName == null) {throw new IllegalStateException("Can't stop StopWatch: it's not running");}long lastTime = System.currentTimeMillis() - this.startTimeMillis;this.totalTimeMillis += lastTime;this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);if (this.keepTaskList) {this.taskList.add(this.lastTaskInfo);}++this.taskCount;this.currentTaskName = null;
}

计时监听器停止,并统计一些任务执行信息。

14、输出日志记录执行主类名、时间信息

if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}

15、发布应用上下文启动完成事件

listeners.started(context);

触发所有 SpringApplicationRunListener 监听器的 started 事件方法。

16、执行所有 Runner 运行器

callRunners(context, applicationArguments);
private void callRunners(ApplicationContext context, ApplicationArguments args) {List<Object> runners = new ArrayList<>();runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());AnnotationAwareOrderComparator.sort(runners);for (Object runner : new LinkedHashSet<>(runners)) {if (runner instanceof ApplicationRunner) {callRunner((ApplicationRunner) runner, args);}if (runner instanceof CommandLineRunner) {callRunner((CommandLineRunner) runner, args);}}
}

执行所有 ApplicationRunner 和 CommandLineRunner 这两种运行器,不详细展开了。

17、发布应用上下文就绪事件

listeners.running(context);

触发所有 SpringApplicationRunListener 监听器的 running 事件方法。

18、返回应用上下文

return context;

总结

Spring Boot 的启动全过程源码分析至此,分析 Spring 源码真是一个痛苦的过程,希望能给大家提供一点参考和思路,也希望能给正在 Spring Boot 学习路上的朋友一点收获。

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

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

相关文章

Spring applicationContext.xml配置文件中配置mybatis使用xml方式

<!-- 定义Mybatis的SqlSessionFactory --><bean id"sqlSessionFactory" class"org.mybatis.spring.SqlSessionFactoryBean"><!-- 定义数据源 --><property name"dataSource" ref"dataSource" /><!-- 指定c…

Vue 2017 现状与展望 | 视频+PPT+速记快速回顾

微软Typescript团队和VS Code团队亲自给Vue开发插件&#xff0c;下一个版本的Vue 2.4将由微软提供支持Vue使用Typescript&#xff0c;之前为VS Code写vue扩展插件的人已入职微软VS Code团队 讲师 | 尤雨溪 速记 | kalasoo 5 月 20 日&#xff0c;在全球首届 VueConf 上&#xf…

从控制台输入一个数 num 与之前定义的数 n 进行比较

//从控制台输入一个数 num 与之前定义的数 n 进行比较&#xff0c;//如果 numn 提示“找到了”//并跳出循环&#xff0c;如果 num<n 提示“小了”&#xff0c;//如果 num>n 提示“大了”,并让其重新输入&#xff0c;//如果 4 次都没有猜对&#xff0c;则提示“抱歉&#…

MySQL多对多创建表语句(防忘记)

-- 演员表 CREATE TABLE actor (id INTEGER NOT NULL -- 演员编号 , NAME VARCHAR(35) -- 演员姓名 , PRIMARY KEY (id) )DEFAULT CHARSETutf8; CREATE INDEX actor_name ON actor(NAME);-- 电影表 CREATE TABLE movie (id INTEGER NOT NULL -- 电影编号 , title VARCHAR(70) -…

TS常用的类型

// 也可以直接使用字面量进行类型声明 let a: 10; a 10;// 可以使用 | 来连接多个类型&#xff08;联合类型&#xff09; let b: "male" | "female"; b "male"; b "female";let c: boolean | string; c true; c hello;// any 表示…

6 道 BATJ 必考的 Java 面试题

转载自 6 道 BATJ 必考的 Java 面试题 题目一 请对比 Exception 和 Error&#xff0c;另外&#xff0c;运行时异常与一般异常有什么区别&#xff1f; 考点分析&#xff1a; 分析 Exception 和 Error 的区别&#xff0c;是从概念角度考察了 Java 处理机制。总的来说&#…

终于知道什么情况下需要实现.NET Core中的IOptions接口

自从接触 IOptions 之后&#xff0c;一直纠结这样的问题&#xff1a;自己定义的 Options 要不要实现 IOptions 接口。 微软有的项目中实现了&#xff0c;比如 Caching 中的 MemoryCacheOptions &#xff1a; public class MemoryCacheOptions : IOptions<MemoryCacheOptio…

有5家衣服专卖店,每家最多购买3件,用户可以选择离开,可以买衣服,最后打印总共买了几件衣服

//有5家衣服专卖店&#xff0c;每家最多购买3件。//用户可以选择离开&#xff0c;可以买衣服。//最后打印总共买了几件衣服 public static void test4(){int count 0;Scanner sc new Scanner(System.in);String an "";//用户没说话&#xff0c;y&#xff0c;买。n…

TS对象中的实例属性和静态属性

// 使用class关键字来定义一个类 /* * 对象中主要包含了两个部分&#xff1a; * 属性 * 方法 * */ class Person{/** 直接定义的属性是实例属性&#xff0c;需要通过对象的实例去访问&#xff1a;* const per new Person();* per.name** 使用st…

Spring开启注解AOP的支持放置的位置

放在springmvc的aop,需要在springmvc的配置文件中写开启aop,而不是spring的配置文件 最近使用aop来记录controller的日志. 但是发现没有起作用. 后来发现是因为我的 aop 是写在controller层(在springmvc的容器中 -web中) 而开启aop配置却写在spring(applicationContext.xml)中…

Amazing ASP.NET Core 2.0

前言 ASP.NET Core 的变化和发展速度是飞快的&#xff0c;当你发现你还没有掌握 ASP.NET Core 1.0 的时候&#xff0c; 2.0 已经快要发布了&#xff0c;目前 2.0 处于 Preview 1 版本&#xff0c;意味着功能已经基本确定&#xff0c;还没有学习过 ASP.NET Core 的同学可以直接…

Myeclipse编辑器简单使用整理

1.重置工作面板&#xff1a; window–>Reset Perspective 2.保存工作面板&#xff1a; window–>Save Perspective 3.切换工作区间&#xff1a; file–>Switch Workspace–>other–>Browse–>选择需要切换到的目录下–》软件自动重启完即可。 4.在myeclipse中…

Java面试常问计算机网络问题

转载自 Java面试常问计算机网络问题 一、GET 和 POST 的区别 GET请注意&#xff0c;查询字符串&#xff08;名称/值对&#xff09;是在 GET 请求的 URL 中发送的&#xff1a;/test/demo_form.asp?name1value1&name2value2 GET 请求可被缓存 GET 请求保留在浏览器历史…

使用DocFX生成文档

文档生成工具DocFX&#xff0c; 类似JSDoc或Sphinx&#xff0c;可以从源代码中提取注释生成文档之外&#xff0c;而且还有语法支持你加入其他的文件链接到API添加额外的说明&#xff0c;DocFX会扫描你的源代码和附加的文件为你生成一个完整的HTML模版网站&#xff0c;你可以自己…

如何在一分钟内搞定面试官

转载自 如何在一分钟内搞定面试官 很多人的求职面试的过程中都会遇到这个问题&#xff1a; “请做个自我介绍。” 有的人&#xff0c;可以口若悬河、妙语连珠讲3分钟&#xff0c;有的人&#xff0c;可能磕磕巴巴&#xff0c;讲了30秒&#xff0c;前者一定能胜过后者&#x…

数组遍历VS对象遍历

数组遍历 const nameArray2 [zhao, qian, zhang, sun, li]; nameArray2.map(() > {}); // return const filterResult nameArray2.filter((value) > {return value zhang; }); // return array nameArray2.forEach(() > {}); // no return对象遍历 Object.keys(o…

logback-spring.xml配置文件

logback-spring.xml配置文件 自己改下value"G:/logs/pmp"这个值&#xff0c;如果你相关依赖弄好的话&#xff0c;直接复制粘贴即用 输出的日志文件的名称最好也改下&#xff0c;下文中${log.path}/web_info.log是因为我这个模块就叫web&#xff0c;要改的话&#x…

基于ZKWeb + Angular 4.0的开源管理后台Demo

这是一套基于ZKWeb网页框架和Angular 4.0编写的开源管理后台Demo&#xff0c;实现了前后端分离和模块化开发&#xff0c; 地址是: https://github.com/zkweb-framework/ZKWeb.MVVMDemo &#xff0c;开源协议是MIT&#xff0c;你可以随意的修改并用于个人或商业用途 我之前已经…

祝我们的所有女孩子,女生节快乐~

公历3月7日是女生节。起源于20世纪90年代初&#xff0c;由山东大学发起&#xff0c;后发展于中国各高校&#xff0c;是一个关爱女生、展现高校女生风采的节日。女神节快乐祝我们18级青鸟1班的女孩儿们&#xff0c;女生节快乐&#xff01;愿快乐与你作陪&#xff0c;美丽将你跟随…

从 0 开始手写一个 Spring MVC 框架,向高手进阶

转载自 从 0 开始手写一个 Spring MVC 框架&#xff0c;向高手进阶 Spring框架对于Java后端程序员来说再熟悉不过了&#xff0c;以前只知道它用的反射实现的&#xff0c;但了解之后才知道有很多巧妙的设计在里面。如果不看Spring的源码&#xff0c;你将会失去一次和大师学习…