没有什么面试经验很容易犯的一个错误就是面试官问一个知识点,你只是回答那一个点,没有深度和广度,像简答题一样一两句就回答完毕。这样的回答,面试基本是凉了。
SpringBoot的初衷
它的诞生是为了简化 Spring 框架初始搭建以及开发的过程,使用它可以不再依赖 Spring 应用程序中的 XML 配置,为更快、更高效的开发 Spring 提供更加有力的支持。
SpringBoot的四个特点
特性一:更快速的构建能力
Spring Boot 提供了更多的 Starters 用于快速构建业务框架,Starters 可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,你可以一站式集成 Spring 及其他技术,而不需要到处找依赖包。
常见的 Starters 有以下几个:
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-starter-thymeleaf
特性二:起步依赖
Spring Boot 提供了起步依赖,也就是在创建 Spring Boot 时可以直接勾选依赖模块,这样在项目初始化时就会把相关依赖直接添加到项目中,大大缩短了查询并添加依赖的时间,
特性三:内嵌容器支持
Spring Boot 内嵌了 Tomcat、Jetty、Undertow 三种容器,其默认嵌入的容器是 Tomcat。
特性四:Actuator 监控
Spring Boot 自带了 Actuator 监控功能,主要用于提供对应用程序监控,以及控制的能力,比如监控应用程序的运行状况,或者内存、线程池、Http 请求统计等,同时还提供了关闭应用程序等功能。
SpringBoot启动流程
public ConfigurableApplicationContext run(String... args) {
// 1.创建并启动计时监控类
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 2.声明应用上下文对象和异常报告集合
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
// 3.设置系统属性 headless 的值
this.configureHeadlessProperty();
// 4.创建所有 Spring 运行监听器并发布应用启动事件
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
// 5.处理 args 参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 6.准备环境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
// 7.创建 Banner 的打印类
Banner printedBanner = this.printBanner(environment);
// 8.创建应用上下文
context = this.createApplicationContext();
// 9.实例化异常报告器
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
// 10.准备应用上下文
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 11.刷新应用上下文
this.refreshContext(context);
// 12.应用上下文刷新之后的事件的处理
this.afterRefresh(context, applicationArguments);
// 13.停止计时监控类
stopWatch.stop();
// 14.输出日志记录执行主类名、时间信息
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), 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);
}
}
1. 创建并启动计时监控类
此计时器是为了监控并记录Spring Boot应用启动的时间的,它记录当前任务的名称,然后开启计时器。
2. 声明应用上下文对象和异常信息报告集合
此过程声明了应用上下文对象和一个异常报告的ArrayList集合。
3. 设置系统属性headless的值
设置Java.awt.headless = true, 其中awt(Abstract Window Toolkit)的含义是抽象窗口工具集。设置为true表示运行一个headless服务器,可以用它来作一些简单的图像处理。
4. 创建所有Spring运行监听器并发布应用启动事件
此过程用于获取配置的监听器名称并实例化所有的类。(应该就是扫描bean的步骤了)
5. 初始化默认应用的参数类
也就是说声明并创建一个应用参数对象
6. 准备环境
创建配置并绑定环境(通过property sources 和 profiles 等配置文件)
7. 创建Banner的打印类
Spring Boot 启动时会打印 Banner 图片,如下图所示:
此 banner 信息是在 SpringBootBanner 类中定义的,我们可以通过实现 Banner 接口来自定义 banner 信息,然后通过代码 setBanner() 方法设置 Spring Boot 项目使用自己自定义 Banner 信息,或者是在 resources 下添加一个 banner.txt,把 banner 信息添加到此文件中,就可以实现自定义 banner 的功能了。
8. 创建应用上下文
根据不同的应用类型来创建不同的ApplicationContext上下文对象。
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 框架了。
本文参考以下:
https://kaiwu.lagou.com/course/courseInfo.htm?courseId=59#/detail/pc?