SpringBoot启动流程

下列源码采用的SpringBoot 2.6.3

启动类

每个SpringBoot项目都有一个启动类,该类用@SpringBootApplication标注着,程序的启动都从这里开始。

java 复制代码
 @SpringBootApplication
 public class ClientApplication {
     public static void main(String[] args) {
         SpringApplication.run(ClientApplication.class, args);
     }
 }
java 复制代码
 public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
         return run(new Class[]{primarySource}, args);
     }
 // 1-先构建SpringApplication  
 // 2-再执行run方法
     public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
         return (new SpringApplication(primarySources)).run(args);
     }

进到run方法中,可以看到它分为2个部分:

  • 执行构造方法-构建SpringApplication
  • run方法的执行

创建SpringApplication

在构造SpringApplication时,部分源代码如下

java 复制代码
 public SpringApplication(Class<?>... primarySources) {
         this((ResourceLoader)null, primarySources);
     }

先传入一个ResourceLoader

java 复制代码
 public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
         this.sources = new LinkedHashSet();
         this.bannerMode = Mode.CONSOLE;
         this.logStartupInfo = true;
         this.addCommandLineProperties = true;
         this.addConversionService = true;
         this.headless = true;
         this.registerShutdownHook = true;
         this.additionalProfiles = Collections.emptySet();
         this.isCustomEnvironment = false;
         this.lazyInitialization = false;
         this.applicationContextFactory = ApplicationContextFactory.DEFAULT;
         this.applicationStartup = ApplicationStartup.DEFAULT;
         this.resourceLoader = resourceLoader;
         Assert.notNull(primarySources, "PrimarySources must not be null");
         this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
   // 获取应用类型
         this.webApplicationType = WebApplicationType.deduceFromClasspath();
   //初始化引导器 bootstrapRegistryInitializers
         this.bootstrapRegistryInitializers = new ArrayList(this.getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
       //设置初始化器
   this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
         //设置监听器
  this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));//定位主类
         this.mainApplicationClass = this.deduceMainApplicationClass();
     }

在构建过程中,大致完成了如下几件事:

  • 设置一些初始值
  • 判断是否加载Servlet,来判断是否是Web环境
  • 初始化启动引导器bootstrapRegistryInitializers
  • 设置初始化器Initializers
  • 设置监听器Listeners
  • 定位主类(根据main方法所在,找到主类)

初始化引导器、初始化器、监听器都是从META-INF/spring.factories文件中定义的名称去查找

运行SpringApplication

run方法

下列为执行run方法的源码:

java 复制代码
 public ConfigurableApplicationContext run(String... args) {
         long startTime = System.nanoTime();
         // 创建-引导上下文 bootstrapContext
         DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
         // 创建配置环境上下文
         ConfigurableApplicationContext context = null;
         // 进入 headless 模式
         this.configureHeadlessProperty();
         // 获取所有运行时 监听器 
         SpringApplicationRunListeners listeners = this.getRunListeners(args);
         listeners.starting(bootstrapContext, this.mainApplicationClass);
 ​
         try {
             // 保存命令行传过来的程序参数
             ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
           // 准备环境 environment
             ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
           // 配置 忽略一些bean
             this.configureIgnoreBeanInfo(environment);
           // 打印banner
             Banner printedBanner = this.printBanner(environment);
             // 创建`IOC`容器
             context = this.createApplicationContext();
             // 设置一个启动器,设置应用程序启动
             context.setApplicationStartup(this.applicationStartup);
           // 准备IOC容器的基本信息
             this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
           // 刷新IOC容器   
           this.refreshContext(context);
           // 执行刷新后的处理
             this.afterRefresh(context, applicationArguments);
             Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
             if (this.logStartupInfo) {
                 (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
             }
             // 通知所有的监听器
             listeners.started(context, timeTakenToStartup);
           // 所有Runner 执行run方法,设置初始化数据
             this.callRunners(context, applicationArguments);
         } catch (Throwable var12) {
             this.handleRunFailure(context, var12, listeners);
             throw new IllegalStateException(var12);
         }
 ​
         try {
             Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
             listeners.ready(context, timeTakenToReady);
             return context;
         } catch (Throwable var11) {
             this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
             throw new IllegalStateException(var11);
         }
     }

流程分析

  • 首先记录一个程序开始时间

  • 创建引导上下文(bootstrapContext)

    • 之前在构造方法时,创建的BootstrapRegistryInitializer就会被加载,用于完成对引导启动上下文的环境设置
  • 创建配置环境上下文(context)

  • 让当前应用进入headless模式。

  • 获取所有 RunListener(运行监听器)【为了方便所有Listener进行事件感知】

    • 遍历 SpringApplicationRunListener 调用 starting 方法,开始监听
java 复制代码
 void starting(ConfigurableBootstrapContext bootstrapContext, Class<?> mainApplicationClass) {
         this.doWithListeners("spring.boot.application.starting", (listener) -> {
             listener.starting(bootstrapContext);
         }, (step) -> {
             if (mainApplicationClass != null) {
                 step.tag("mainApplicationClass", mainApplicationClass.getName());
             }
 ​
         });
     }
  • 保存命令行传过来的程序参数(它优先于项目里面的配置),如: java -jar --spring.profiles.active=prod

  • 准备环境 prepareEnvironment

    • 创建环境
    • 读取所有的配置源的配置属性值
    • 绑定环境信息
    • 监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成
    • 绑定当前环境EnvironmentSpringApplication
java 复制代码
 private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
         ConfigurableEnvironment environment = this.getOrCreateEnvironment();
         this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());
         ConfigurationPropertySources.attach((Environment)environment);
         listeners.environmentPrepared(bootstrapContext, (ConfigurableEnvironment)environment);
         DefaultPropertiesPropertySource.moveToEnd((ConfigurableEnvironment)environment);
         Assert.state(!((ConfigurableEnvironment)environment).containsProperty("spring.main.environment-prefix"), "Environment prefix cannot be set via properties.");
         this.bindToSpringApplication((ConfigurableEnvironment)environment);
         if (!this.isCustomEnvironment) {
             environment = this.convertEnvironment((ConfigurableEnvironment)environment);
         }
 ​
         ConfigurationPropertySources.attach((Environment)environment);
         return (ConfigurableEnvironment)environment;
     }
  • 配置忽略的 bean

  • 打印banner

  • 根据项目类型(Servlet)创建IOC容器

  • 设置一个启动器,应用程序启动

  • 配置IOC容器的基本信息 prepareContext()

    • 保存环境信息
    • IOC容器的后置处理流程
    • 应用初始化器;applyInitializers,来对容器进行初始化扩展 (把在构建SpringApplication时,创建的Initializers,全部初始化)
    • 最后所有监听器 加载配置环境上下文 ------listeners.contextLoaded(context);
  • 刷新IOC容器,refreshContext()

    • 创建容器中所有组件(涉及容器启动跟自动装配)
  • 执行刷新后的操作 afterRefresh()

    • 本身没有内容,是留给用户自定义容器刷新完成后的处理逻辑
  • 所有监听器 调用 listeners.started(context); 通知所有的监听器 ioc已经ok

  • 调用所有runners;callRunners()

    • 获取容器中的 ApplicationRunner

    • 获取容器中的 CommandLineRunner

    • 合并所有runner并且按照@Order进行排序

    • 遍历所有的runner。调用 run 方法

    Spring中有两种Runner,ApplicationRunnerCommandLineRunner.它们都是接口

    它的作用是进行一些初始化的操作,比如预先加载并缓存某些数据,读取某些配置等等。

    这两个接口可以在 Spring 的环境下指定一个 Bean 运行(run)某些你想要做的事情,如果你有多个 Bean 进行指定,那么可以通过 Ordered 接口或者 @Order 注解指定执行顺序。

  • 所有监听器 listeners.ready(context, timeTakenToReady);告知一切准备就绪,

概要

  • 准备一些环境跟配置信息
  • 创建启动引导上下文、环境配置上下文
  • 启动所有监听器-进行监听
  • 准备ioc容器(准备ioc环境,创建ioc容器,最后刷新它)
  • 程序中的Runner执行run方法
相关推荐
杨哥带你写代码19 分钟前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
郭二哈1 小时前
C++——模板进阶、继承
java·服务器·c++
A尘埃1 小时前
SpringBoot的数据访问
java·spring boot·后端
yang-23071 小时前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端
沉登c1 小时前
幂等性接口实现
java·rpc
代码之光_19801 小时前
SpringBoot校园资料分享平台:设计与实现
java·spring boot·后端
科技资讯早知道2 小时前
java计算机毕设课设—坦克大战游戏
java·开发语言·游戏·毕业设计·课程设计·毕设
苹果醋32 小时前
快速玩转 Mixtral 8x7B MOE大模型!阿里云机器学习 PAI 推出最佳实践
spring boot·nginx·毕业设计·layui·课程设计
程序员大金3 小时前
基于SpringBoot+Vue+MySQL的装修公司管理系统
vue.js·spring boot·mysql
小比卡丘3 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言