SpringBoot06-启动原理

SpringBoot-启动原理

Spring启动时做了哪些事

SpringBoot启动流程
1
return (new SpringApplication(primarySources)).run(args);

可以看出SpringBoot启动的过程可以分成2步

1、new SpringApplication()

1
2
3
4
5
6
7
8
9
10
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.bootstrapRegistryInitializers = getBootstrapRegistryInitializersFromSpringFactories();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
  1. 记录springboot启动类
  2. 判断SpringBoot启动环境是否web
  3. 从META-INF/spring.factories 获取应用BootstrapRegistryInitializer初始化器
  4. 从META-INF/spring.factories 获取应用上下文ApplicationContextInitializer初始化器
  5. 从META-INF/spring.factories 获取应用监听器
  6. 获取springboot启动类main方法所在类

2、SpringApplication.run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public ConfigurableApplicationContext run(String... args) {
//计时器
StopWatch stopWatch = new StopWatch();
stopWatch.start();
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
//声明容器
ConfigurableApplicationContext context = null;
configureHeadlessProperty();
//获取所有SpringApplicationRunListener,用于监听spiringboot生命周期
SpringApplicationRunListeners listeners = getRunListeners(args);
//启动-发布启动事件
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//准备容器环境-发布环境准备事件
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
configureIgnoreBeanInfo(environment);
//打印springboot的图标
Banner printedBanner = printBanner(environment);
//创建容器 根据webApplicationType 来创建容器 通过反射创建
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
//准备环境
//1:把环境设置到容器中
//2: 循环调用AppplicationInitnazlier 进行容器初始化工作
//3:发布容器上下文准备完成事件
//4:注册关于springboot特性的相关单例Bean
//5:发布容器上下文加载完毕事件
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
//运行 ApplicationRunner 和CommandLineRunner
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}

try {
//发布容器启动事件
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}

SpringBoot启动的2种方式

jar包

​ 本质:SpringBoot启动带动内置tomcat启动

​ 简述:SpringApplication.run

​ ->新建ioc

​ -> 容器刷新

​ -> onrefresh方法

​ ->ServletWebServerApplicationContext

​ ServletWebServerFactory factory = getWebServerFactory();

​ factory.getWebServer(getSelfInitializer());

jar启动流程

springboot启动流程图

war包

​ 本质:tomcat启动带动SpringBoot启动(Servlet3.0特性 tomcat启动会创建)

8.2.4 Shared libraries / runtimes pluggability
The ServletContainerInitializer class is looked up via the jar services API. For each application, an instance of the ServletContainerInitializer is created by the container at application startup time. The framework providing an implementation of the ServletContainerInitializer MUST bundle in the META-INF/services directory of the jar file a file called javax.servlet.ServletContainerInitializer, as per the jar services API,that points to the implementation class of the ServletContainerInitializer. In addition to the ServletContainerInitializer we also have an annotation -HandlesTypes. The HandlesTypes annotation on the implementation of the ServletContainerInitializer is used to express interest in classes that may have annotations (type, method or field level annotations) specified in the value of the HandlesTypes or if it extends / implements one those classes anywhere in the class’ super types. The HandlesTypes annotation is applied irrespective of the setting of metadata-complete.

1.1)web应用启动,会创建当前Web应用导入jar包中 的 ServletContainerInitializer类的实例

1.2)ServletContainerInitializer 类必须放在jar包的 META-INF/services目录 下,文件名称为 javax.servlet.ServletContainerInitializer

1.3)文件的内容指向ServletContainerInitializer实现类的全路径

1.4)使用@HandlesTypes 在我们应用启动的时候,加载我们感兴趣的类

1.5)spring-web 配置了tomcat启动时要创建的实例类

1
2
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {

​ 简述:Tomcat启动->

​ SpringServletContainerInitializer.onStartup

​ ->SpringBootServletInitializer.onStartup

​ ->WebApplicationContext rootApplicationContext = createRootApplicationContext(servletContext)

​ ->run(application) 触发SpringBoot启动

war启动流程

springboot启动War包