springboot启动流程详解(springboot三种启动方法)

电脑入门知识大全 电脑知识 397

一切从SpringApplication.run()开始,最终返回一个ConfigurableApplicationContext

springboot启动流程详解(springboot三种启动方法)-第1张图片-90博客网

构造了一个SpringApplication对象,然后调用它的run方法。下面看下构造方法都做了什么

springboot启动流程详解(springboot三种启动方法)-第2张图片-90博客网

1、推断Web应用类型(WebApplicationType),本例中是SERVLET

2、设置 Bootstrapper、ApplicationContextInitializer 和 ApplicationListener

3、推断main方法

注意到,在设置 Bootstrapper、ApplicationContextInitializer 和 ApplicationListener 时,都调用了同一个方法getSpringFactoriesInstances

springboot启动流程详解(springboot三种启动方法)-第3张图片-90博客网

springboot启动流程详解(springboot三种启动方法)-第4张图片-90博客网

springboot启动流程详解(springboot三种启动方法)-第5张图片-90博客网

从所有 META-INF/spring.factories 文件中加载,并查找指定名称的的值

springboot启动流程详解(springboot三种启动方法)-第6张图片-90博客网

回到之前的构造方法那里,现在我们知道设置的Bootstrapper、ApplicationContextInitializer、ApplicationListener是从哪儿来的了,原来是在META-INF/spring.factories中配置的。查找指定key对应的值,然后实例化它们,并返回一个List

springboot启动流程详解(springboot三种启动方法)-第7张图片-90博客网

接下来,重头戏,调用run方法

springboot启动流程详解(springboot三种启动方法)-第8张图片-90博客网

重点看try里面的内容

首先是启动监听器来监听启动过程,这里再一次调用了熟悉的getSpringFactoriesInstances

springboot启动流程详解(springboot三种启动方法)-第9张图片-90博客网

然后,将main方法中传的参数包装成ApplicationArguments对象

紧接着,创建并配置环境,返回一个ConfigurableEnvironment对象,由于本例中是SERVLET,所以创建的是StandardServletEnvironment

然后,打印Banner

创建ApplicationContext,所有基于Spring的项目启动过程都是创建ApplicationContext

springboot启动流程详解(springboot三种启动方法)-第10张图片-90博客网

这里创建的ConfigurableApplicationContext是AnnotationConfigServletWebServerApplicationContext

有了ApplicationContext,紧接着要配置ApplicationContext

springboot启动流程详解(springboot三种启动方法)-第11张图片-90博客网

相关影片资源迅雷下载推荐

win10怎么更改开机用户名(win10怎么更改账户名称)

加载所有的source(其实就是class),然后实例化

springboot启动流程详解(springboot三种启动方法)-第12张图片-90博客网

这里就是一个,就是启动类

springboot启动流程详解(springboot三种启动方法)-第13张图片-90博客网

springboot启动流程详解(springboot三种启动方法)-第14张图片-90博客网

接下来,刷新容器

springboot启动流程详解(springboot三种启动方法)-第15张图片-90博客网

springboot启动流程详解(springboot三种启动方法)-第16张图片-90博客网

最终是调用Spring的applicationContext.refresh(),又看到了熟悉的一幕

springboot启动流程详解(springboot三种启动方法)-第17张图片-90博客网

最后还留了一个入口,回调子容器的onRefresh()

springboot启动流程详解(springboot三种启动方法)-第18张图片-90博客网

Spring MVC的启动是创建父子两个WebApplicationContext

在刷新容器之后,所有的Bean都已经实例化完成

springboot启动流程详解(springboot三种启动方法)-第19张图片-90博客网

afterRefresh什么也没做

最后是回调Runner,回调所有的ApplicationRunner和CommandLineRunner

springboot启动流程详解(springboot三种启动方法)-第20张图片-90博客网

至此,Spring Boot启动完成

回顾一下,

1、推断Web应用类型

2、设置Bootstrapper、ApplicationContextInitializer、ApplicationListener

3、推断main方法

4、创建并配置环境Environment

5、打印Banner

6、创建并配置ApplicationContext

7、刷新ApplicationContext(此处交由Spring处理,调用Spring的refresh()方法)实例化所有的bean

8、回调所有的ApplicationRunner和CommandLineRunner


相关影片资源迅雷下载推荐

uefi安装win10系统详细过程(uefi安装win10原版镜像详细步骤)

抱歉,评论功能暂时关闭!