SpringBoot的启动过程--实现打印炫酷控制台图案SpringBoot的启动过程:
我们知道 Spring Boot 程序的⼊⼝是 SpringApplication.run(Application.class, args) ⽅法,那么就从 run() ⽅法开始分析吧,它的源
码如下:
public ConfigurableApplicationContext args) {
// 1.创建并启动计时监控类
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 2.声明应⽤上下⽂对象和异常报告集合
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
// 3.设置系统属性 headless 的值
// 4.创建所有 Spring 运⾏并发布应⽤启动事件
SpringApplicationRunListeners listeners = RunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
// 5.处理 args 参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 6.准备环境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
/
/ 7.创建 Banner 的打印类
Banner printedBanner = this.printBanner(environment);
// 8.创建应⽤上下⽂
context = ateApplicationContext();
// 9.实例化异常报告器
exceptionReporters = SpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);        // 10.准备应⽤上下⽂
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 11.刷新应⽤上下⽂
// 12.应⽤上下⽂刷新之后的事件的处理
this.afterRefresh(context, applicationArguments);
// 13.停⽌计时监控类
stopWatch.stop();
// 14.输出⽇志记录执⾏主类名、时间信息
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).ApplicationLog(), stopWatch);
}
// 15.发布应⽤上下⽂启动完成事件
listeners.started(context);
// 16.执⾏所有 Runner 运⾏器
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
// 17.发布应⽤上下⽂就绪事件
listeners.running(context);
// 18.返回应⽤上下⽂对象
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
从以上源码可以看出 Spring Boot 的启动总共分为以下 18 个步骤。
Spring Boot 的启动流程
1.创建并启动计时监控类
此计时器是为了监控并记录 Spring Boot 应⽤启动的时间的,它会记录当前任务的名称,然后开启计时器。
2.声明应⽤上下⽂对象和异常报告集合
此过程声明了应⽤上下⽂对象和⼀个异常报告的 ArrayList 集合。
3.设置系统属性 headless 的值
设置 Java.awt.headless = true,其中 awt(Abstract Window Toolkit)的含义是抽象窗⼝⼯具集。设置为 true 表⽰运⾏⼀个headless 服务器,可以⽤它来作⼀些简单的图像处理。
4.创建所有 Spring 运⾏并发布应⽤启动事件
此过程⽤于获取配置的名称并实例化所有的类。
5.初始化默认应⽤的参数类
也就是说声明并创建⼀个应⽤参数对象。
6.准备环境
创建配置并且绑定环境(通过 property sources 和 profiles 等配置⽂件)。
7.创建 Banner 的打印类
Spring Boot 启动时会打印 Banner 图⽚,如下图所⽰:
9.实例化异常报告器
它调⽤的是 getSpringFactoriesInstances() ⽅法来获取配置异常类的名称,并实例化所有的异常处理类。
10.准备应⽤上下⽂
此⽅法的主要作⽤是把上⾯已经创建好的对象,传递给 prepareContext 来准备上下⽂,例如将环境变量 environment 对象绑定到上下⽂中、配置 bean ⽣成器以及资源加载器、记录启动⽇志等操作。
11.刷新应⽤上下⽂
此⽅法⽤于解析配置⽂件,加载 bean 对象,并且启动内置的 web 容器等操作。
12.应⽤上下⽂刷新之后的事件处理
这个⽅法的源码是空的,可以做⼀些⾃定义的后置处理操作。
13.停⽌计时监控类
停⽌此过程第⼀步中的程序计时器,并统计任务的执⾏信息。
14.输出⽇志信息
把相关的记录信息,如类名、时间等信息进⾏控制台输出。
15.发布应⽤上下⽂启动完成事件
触发所有 SpringApplicationRunListener 的 started 事件⽅法。
16.执⾏所有 Runner 运⾏器
执⾏所有的 ApplicationRunner 和 CommandLineRunner 运⾏器。
17.发布应⽤上下⽂就绪事件
触发所有的 SpringApplicationRunListener 的 running 事件。
18.返回应⽤上下⽂对象
到此为⽌ Spring Boot 的启动程序就结束了,我们就可以正常来使⽤ Spring Boot 框架了。控制台打印的 ”Spring” 图案是在哪⾥打印的?如何修改?在run⽅法⾥的:
//我们发现这⼀步是处理启动banner的逻辑,点进去看看
springboot框架的作用
Banner printedBanner = this.printBanner(environment);
看到读取了⼀个的⽂件,如果没有 ⽂件则读取默认DEFAULT_BANNER
如果有 ⽂件则会读取⽂件内容并打印到控制台
⽽如果想打印⾃定义的图案则只需在项⽬的resources下新增⽂件,⽂件内既是要在控制台打印的内容

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。