几个重要的事件回调机制

  • ApplicationContextInitializer
  • SpringApplicationRunListener
  • ApplicationRunner
  • CommandLineRunner

启动流程:

  1. 创建SpringApplication对象

    /**
    * 在springboot 2以前:
    * 调用initialize(source);
    * 在springboot 2以后,跳过了initialize()方法,直接调用run()方法
    */ private void initialize(Object[] source){
    // 保存主配置类
    if(sources != null && source.length > 0){
    this.sources.addAll(Arrays.asList(sources));
    }
    // 判断当前是否是一个web应用
    this.webEnvironment = deduceWebEnvironment();
    // 从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
    setInitializes((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    // 从类路径下找到META-INF/spring.factories配置的所有ApplicationListener;然后保存起来
     setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    // 从多个配置类中找到有main方法的主配置类
    this.mainApplicationClass = deduceMainApplicationClass();
    }
  2. 运行run方法
    public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty(); // 获取SpringApplicationRunListener;从类路径下META-INF/spring.factories找到
    SpringApplicationRunListeners listeners = getRunListeners(args);
    // 回调所有的SpringApplicationRunListener.starting()方法
    listeners.starting();
    try {
    // 封装命令行参数
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    // 准备环境
    ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
    // 创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成
    configureIgnoreBeanInfo(environment);
    Banner printedBanner = printBanner(environment); // 创建ApplicationContext;决定创建web的ioc还是普通的ioc
    context = createApplicationContext();
    exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
    new Class[] { ConfigurableApplicationContext.class }, context);
    // 准备上下文环境;将environment保存到ioc中
    // applyInitializers();回调之前保存的所有的SpringApplicationRunListener的initializers方法 prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    // 回调所有的SpringApplicationRunListener的contextPrepared();
    // 刷新容器;ioc容器初始化完成
    refreshContext(context);
    afterRefresh(context, applicationArguments);
    stopWatch.stop();
    if (this.logStartupInfo) {
    new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    }
    listeners.started(context);
    callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
    handleRunFailure(context, ex, exceptionReporters, listeners);
    throw new IllegalStateException(ex);
    } try {
    listeners.running(context);
    }
    catch (Throwable ex) {
    handleRunFailure(context, ex, exceptionReporters, null);
    throw new IllegalStateException(ex);
    }
    return context;
    }

SpringBoot 启动配置原理的更多相关文章

  1. springboot启动配置原理之二(运行run方法)

    public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); s ...

  2. springboot 启动配置原理【转】【补】

    创建应用 几个重要的事件回调机制  , 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunL ...

  3. springboot启动配置原理之一(创建SpringApplication对象)

    几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...

  4. springboot启动配置原理之三(事件监听机制)

    ApplicationContextInitializer public class HelloApplicationContextInitializer implements Application ...

  5. SpringBoot启动过程原理

    最近这两年springboot突然火起来了,那么我们就来看看springboot的运行原理. 一.springboot的三种启动方式: 1.运行带有main方法的2.通过命令 Java -jar命令3 ...

  6. 这样讲 SpringBoot 自动配置原理,你应该能明白了吧

    https://juejin.im/post/5ce5effb6fb9a07f0b039a14 前言 小伙伴们是否想起曾经被 SSM 整合支配的恐惧?相信很多小伙伴都是有过这样的经历的,一大堆配置问题 ...

  7. 【SpringBoot1.x】SpringBoot1.x 启动配置原理 和 自定义starter

    SpringBoot1.x 启动配置原理 和 自定义starter 启动配置原理 本节源码 启动过程主要为: new SpringApplication(sources) 创建 SpringAppli ...

  8. springboot自动配置原理以及手动实现配置类

    springboot自动配置原理以及手动实现配置类 1.原理 spring有一个思想是"约定大于配置". 配置类自动配置可以帮助开发人员更加专注于业务逻辑开发,springboot ...

  9. SpringBoot自动配置原理

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾前面Spring的文章(以学习的顺序排好): S ...

随机推荐

  1. [CSharp]传一个包含多个属性的对象,只改变其中个别属性值的方法

    需求 假如有这么一个需求,一个对象Person内的属性设置外包给了另外一个类Options, 而要设这个Person对象的属性,就必须传一个Options实例, 但又不能每个属性重新设一遍,只设要修改 ...

  2. C#基础之方法的重载

    在C#语言中,方法的重载作用非常大,但是使用重载需要注意方法的签名,必须有一种要不一样,具体指的是: 1.方法的返回值类型 2.方法的形参类型 3.形参类型的顺序 4.形参的个数 4.泛型的类型< ...

  3. unicode、encode、decode

    1.encode与decode:unicode经过encode -> utf-8,反过来为decode. 爬虫读取网页内容和pandas读取csv时,会把读取到的文字内容转成unicode,当我 ...

  4. 【MOOC操作系统】测试题大题-进程调度 先入先服务算法例题 【某多道程序系统供用户使用的主存为100K,磁带机2台,打印机1台,采用可变分区存储管理,静态方式分配外围设备(进程获得所需全部设备才能进入内容),忽略用户作业的I/O时间。采用动态分区、首次匹配法(从低地址区开始)分配主存,一个作业创建一个进程,且运行中不紧缩内存。作业调度采用FCFS算法,在主存中的进程采用剩余时间最短调度算法。】

    分析图: 答案: (1) 8 : 00作业1到达,占有资源并调入主存运行. 8: 20作业2和3同时到达,但作业2因分不到打印机,只能在后备队列等待.作业3资源满足,可进主存运行,并与作业1平分CPU ...

  5. RabbitMQ学习笔记一

    前 言 -解决问题  一.RabbitMQ安装  1.安装erlang 环境 a.下载erlang 版本,注意这里需要和安装的rabbitMq版本相配对,rabbitMQ官方网站上可以查到:https ...

  6. tomcat漏洞利用总结

    一.后台war包getshell 漏洞利用: tomcat在conf/tomcat-users.xml配置用户权限 <?xml version="1.0" encoding= ...

  7. 关于STL-map容器

    1.使用时加入头文件#include <map>; 2.从前遍历it = map.begin(); it != map.end(); it++ 3.从后遍历it = map.rbegin( ...

  8. Beta冲刺 —— 5.31

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.讨论并解决每个人存在的问 ...

  9. C语言深入理解通过指针引用多维数组(指针中使用起始地址 元素地址 元素值的区分)

    #include "pch.h" #include <iostream> #include<stdio.h> int main() { // std::co ...

  10. Java实现 黑洞数

    任意一个5位数,比如:34256,把它的各位数字打乱,重新排列,可以得到一个最大的数:65432,一个最小的数23456.求这两个数字的差,得:41976,把这个数字再次重复上述过程(如果不足5位,则 ...