几个重要的事件回调机制

  • 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. 【项目练习】thinkphp用户注册

    使用mvc,ajax 路由 //后台登陆 Route::group('admin', function () { Route::rule('login', 'admin/Index/login'); ...

  2. Linux显示行号设置

    linux显示行号设置 第一步,打开vim vi ~/.vimrc 第二步,在该文件中加入一行,命令如下: set nu # 显示行号 set nonu # 不显示行号 微信公众号:喵哥解说 公众号介 ...

  3. JSP指令 & 中文乱码问题

    1. JSP 指令: JSP指令(directive)是为JSP引擎而设计的,     它们并不直接产生任何可见输出, 而只是告诉引擎如何处理JSP页面中的其余部分. 2. 在JSP 2.0中,定义了 ...

  4. neo4j-jdbc driver

    https://github.com/neo4j-contrib/neo4j-jdbc/releases/tag/3.3.1

  5. C# 根据BackgroundWoker异步模型和ProgressBar控件,自定义进度条控件

    前言 程序开发过程中,难免会有的业务逻辑,或者算法之类产生让人能够感知的耗时操作,例如循环中对复杂逻辑处理;获取数据库百万乃至千万级数据;http请求的时候等...... 用户在使用UI操作并不知道程 ...

  6. Vim的三款实用插件

    Vim 是 Linux 下的常用文本编辑器,但也经常被称为是一个上古神器,因为它对于初学者而言相当不友好,也不好入门. 但是,对于高手而言,他们不仅将 Vim 玩得很溜,而且还将它当作代码开发的主要工 ...

  7. Weblogic 漏洞利用总结

    整理的一些利用方式,后续会更新到15年至今的洞 后台爆破拿shell 后台爆破: 部署-安装-上载文件 制作war包,一直下一步,最好保存 jar -cvf shell.war ./shll/* 上传 ...

  8. leetcode976之三角形最大周长

    题目描述: 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. 如果不能形成任何面积不为零的三角形,返回 0. def largePara(A): A. ...

  9. 【算法】单元最短路径之Bellman-Ford算法和SPFA算法

    SPFA是经过对列优化的bellman-Ford算法,因此,在学习SPFA算法之前,先学习下bellman-Ford算法. bellman-Ford算法是一种通过松弛操作计算最短路的算法. 适用条件 ...

  10. 本地计算机上的MySQL80服务启动后停止,某些服务在未由其他服务或者程序使用时将自动停止

    是由于mysql server XX 路径下的my.ini文件发生错误. 高版本的mysql server的my.ini文件不在mysql server XX路径下,在programdata文件夹(查 ...