1、前言
springboot清楚的流程解析
2、springboot的启动
2.1 Demo工程准备
2.1.1 创建一个Demo工程
利用idea通过自带的https://start.spring.io创建demo工程,并勾选web选项
2.1.2 运行主函数
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
: Starting DemoApplication on fanddeMacBook-Pro.local with PID 53222 (/Users/fand/JavaEE/git/demo/target/classes started by fand in /Users/fand/JavaEE/git/demo)
: No active profile set, falling back to default profiles: default
: Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
: For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
: Multiple Spring Data modules found, entering strict repository configuration mode!
: Bootstrapping Spring Data repositories in DEFAULT mode.
: Finished Spring Data repository scanning in 14ms. Found 0 repository interfaces.
: Tomcat initialized with port(s): 8080 (http)
: Starting service [Tomcat]
: Starting Servlet engine: [Apache Tomcat/9.0.22]
: Initializing Spring embedded WebApplicationContext
: Root WebApplicationContext: initialization completed in 1561 ms
: Initializing ExecutorService 'applicationTaskExecutor'
: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
: LiveReload server is running on port 35729
: Tomcat started on port(s): 8080 (http) with context path ''
: Started DemoApplication in 2.991 seconds (JVM running for 3.448)
: A cookie header was received [1566368901,1567582917; JSESSIONID=247047835B9342FB6EA0882C433E2C11; Hm_lpvt_cdce8cda34e84469b1c8015204129522=1567606620] that contained an invalid cookie. That cookie will be ignored.
Note: further occurrences of this error will be logged at DEBUG level.
: Initializing Spring DispatcherServlet 'dispatcherServlet'
: Initializing Servlet 'dispatcherServlet'
: Completed initialization in 6 ms
2.1.3 浏览器访问
2.2 启动流程分析
概述
Springboot的启动过程,简而言之就是一系列对象创建及方法执行的过程,这些对象既包括启动类对象SpringApplication及其属性对象,也包括Spring容器当中的单实例组件的创建过程,如果我们将SpringBoot启动链路上这些关键对象的创建过程分析透彻了,那么Springboot的启动流程自然也就明白了, 那么这些关键对象有哪些呢?
- ApplicationContextInitializer - classpath:/META-INF/spring.factories 中配置声明
- ApplicationListener - classpath:/META-INF/spring.factories 中配置声明
- SpringApplicationRunListeners - classpath:/META-INF/spring.factories 中配置声明
- ConfigurableEnvironment - 根据当前项目是否是Web/Reactive(响应式)/其他 来判断分别创建StandardServletEnvironment、StandardReactiveWebEnvironment、StandardEnvironment类型
- Banner - banner打印相关
- ConfigurableApplicationContext - Spring应用上下文对象根据当前项目是否是Web/Reactive(响应式)/其他 来判断分别创建AnnotationConfigServletWebServerApplicationContext、AnnotationConfigReactiveWebServerApplicationContext、AnnotationConfigApplicationContext类型
- ApplicationRunner - Springboot应用启动起来的后置处理bean,需要使用@Component(及其他相同功能的注解)将其纳入到Spring容器中,Springboot启动完成之后,会调用Spring容器中的ApplicationRunner的run方法,执行后置的处理,会在CommandLineRunner之前执行
- CommandLineRunner- Springboot应用启动起来的后置处理bean,需要使用@Component(及其他相同功能的注解)将其纳入到Spring容器中,Springboot启动完成之后,会调用Spring容器中的CommandLineRunner的run方法,执行后置的处理
下面开始分步骤分析Springboot的启动流程,首先进入项目主程序入口
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
简单来说,上述主方法中,springboot干了两件事情:
一、创建一个SpringApplication对象,并初始化ApplicationContextInitializer和ApplicationListener
方法的调用栈如下所示,(mainApplicationClass将被初始化为上面main方法所在的class类,DemoApplication.class)
**二、执行上述SpringApplication对象的run方法,开始启动springboot应用
通过进入run方法,程序来到springboot启动的主流程函数SpringApplication.run,run方法即是Springboot应用启动的全过程了,了解了run方法的执行流程,也就掌握了Springboot的启动流程。
下面将分步骤对run执行的关键步骤进行分别解析,了解springboot启动的核心内容