SpringBoot启动流程原理

spring boot的启动流程

一、初始化SpringAppliaction对象

在这里插入图片描述

1. 设置应用的容器类型

当前的web应用类型webApplicationType)是在SpringApplication构造函数中设置的,设置的逻辑在WebApplicationType.deduceFromClasspath中:
在这里插入图片描述

   return WebApplicationType.SERVLET;

可以看出SpringBoot将应用程序分为三种类型:

  1. Reactive
    Spring团队推出的Reactor编程模型的非阻塞异步Web编程框架WebFlux
  2. Servlet
    基于J2EE Servlet API的编程模型,运行在Servlet容器上
  3. None
    非Web应用程序

通过类路径中是否存在WebFlux中的DispatcherhandlerSpringMVC中的DispatcherServletServletConfigurableWebApplicationContext来推断Web应用程序类型

2. 收集加载资源(比如ApplicationContextInitializer、ApplicationListener)

A. 将ApplicationContextInitializer在spring.factories文件中对应的类全部实例化。

B. 将ApplicationListener在spring.factories文件中对应的类全部实例化。

在这里插入图片描述
在这里插入图片描述

	public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

二 、run()方法
1. 创建并配置当前Spring Boot应用将要使用的Environment,加载application.yml文件

通过遍历ApplicationListener所有实例,真正实现加载application.yml文件的是org.springframework.boot.context.config.ConfigFileApplicationListenter

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2. 根据环境创建对应容器

printBanner()打印springboot旗帜

在这里插入图片描述

3. 将之前通过@EnableAutoConfiguration获取的所有配置以及其他形式的IoC容器配置加载到已经准备完毕的ApplicationContext

启动类中@SpringBootApplication是个组合注解,即

  1. @Configuration

    声明当前类是一个配置类,相当于xml文件。

  2. @ComponentScan

    自动扫描需要装配的类,并自动装配到Spring的Bean容器中

  3. @EnableAutoFiguration

在EnableAutoConfiguration中定义了两个属性

a. exclude和excludeName,用来排除一个自动配置的类

b. 最关键的是@import(EnableAutoConfigurationImportSelector.class)

@import的作用是导入一个或多个配置类。EnableAutoCongigurationImportSelector实现了 ImpostSelector接口,这个接口中定义了 selectImport方法,这个方法是用来扫描并返回需要导入的类的。

selectImports方法是去获取自动配置类的,里面最重要的一行代码就是getCandidateConfiguration方法。

这个getCandidateConfiguration方法会返回EnableAutoConfiguration类,是通过SpringFactoriesLoader.loadFactoryNames来实现的。

这个方法里就会读取classpath下所有META-INF/spring.factories文件中的配置,然后返回一个字符串数组。里面就定义了很多自动配置的类,把这些信息读取到并返回之后,就会把这些XxxAutoConfiguration注入到Spring容器中。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值