springboot run方法执行流程详解(一)——springApplication初始化及部分run方法

上篇博客介绍了springboot的自动配置原理,这一篇我们就研究一下springboot的run方法,到底是如何运行的。

springboot版本:2.1.1.RELEASE

今天我们就以debug的形式,跟着run方法走一遍。

启动mian方法,进入到ConfigurableApplicationContext.run方法:
在这里插入图片描述
这部分主要有两个操作,第一就是初始化springApplication,第二就是执行run方法,我们先看初始化springApplication都做了那些操作。

这里是引用
我们可以看到,这步操作只是对springApplication进行了初始化的赋值,其中比较复杂的是其中的setInitializers((this.getSpringFactoriesInstances(ApplicationContextInitializer.class))

setListeners(this.getSpringFactoriesInstances(ApplicationListener.class))
操作,我们以setInitializers()为例,点进去看下内部进行了什么操作:

在这里插入图片描述
我们又看到了熟悉的SpringFactoriesLoader.loadFactoryNames()方法,没错,这步就是到类路径下找到META-INF/spring.factories文件,然后再从中找出对应ApplicationContextInitializer的全路径数组,并将bean的名称以Set形式返回,然后通过createSpringFactoriesInstances()方法,将bean实例化,并将实例List返回并set到springApplication对应的属性中。同理,setListeners()方法也是这么操作的,只不过将从spring.factories文件中查找的ApplicationContextInitializer换成了ApplicationListener。

以上就是springApplication初始化的大致操作,下面我们着重看下run方法的执行流程。

  public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
    this.configureHeadlessProperty();
    SpringApplicationRunListeners listeners = this.getRunListeners(args);
    listeners.starting();

    Collection exceptionReporters;
    try {
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
      ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
      this.configureIgnoreBeanInfo(environment);
      Banner printedBanner = this.printBanner(environment);
      context = this.createApplicationContext();
      exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
      this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
      this.refreshContext(context);
      this.afterRefresh(context, applicationArguments);
      stopWatch.stop();
      if (this.logStartupInfo) {
        (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
      }

      listeners.started(context);
      this.callRunners(context, applicationArguments);
    } catch (Throwable var10) {
      this.handleRunFailure(context, var10, exceptionReporters, listeners);
      throw new IllegalStateException(var10);
    }

    try {
      listeners.running(context);
      return context;
    } catch (Throwable var9) {
      this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
      throw new IllegalStateException(var9);
    }
  }

上面是run方法的源码,我们来逐条分析一下,里面做了哪些操作

1

  StopWatch stopWatch = new StopWatch();
  stopWatch.start();

这个代码很简单,就是创建一个spring秒表计时器,并开始计时。

2

this.configureHeadlessProperty();

这个方法我们点进去看下:
在这里插入图片描述
这个方法的含义就是给属性设值System.setProperty(),它的值来源于System.getProperty(),奇怪了,为什么把属性从一个地方取出来,然后又设置到同一个地方,这不是多此一举吗?
其实这是因为System中的两个读写属性的方法不对等.
System中getProperty()有2个重载方法,但却只有一个setProperty()方法,其中getProperty()有单参和双参两方法,单参就是简单的获取属性,有就有,没有就没有,双参则聪明一点,在没有的时候会返回一个调用者指定的默认值,所以经过这样操作后,不管有没有那个属性,最终都能保证有.
上文引用自博客SpringBoot启动源码探究----configureHeadlessProperty()方法

3

 SpringApplicationRunListeners listeners = this.getRunListeners(args);
 listeners.starting();

又看到了熟悉的getRunListeners方法,原理是和初始化springApplication时的操作一样的,只不过在加载监听器之后,又启动了监听器,这里就不再重复阐述了,忘记的小伙伴可以看上面的springApplication初始化解释。

4

  ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);

这步主要是进行准备 Spring 环境,我们点进去看下具体实现:
在这里插入图片描述
ConfigurableEnvironment environment = this.getOrCreateEnvironment();,这个方法作用是创建一个环境对象,内部会做个判断,从而判断是创建web环境对象,还是标准非web环境对象。如果创建的是web环境对象的话,最终会调用下面的的方法:
在这里插入图片描述
这里是进行了系统环境变量的初始化。
让我们回到prepareEnvironment方法中,看下
this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());这一步做了什么操作:
在这里插入图片描述
上图中的configurePropertySources方法,我们debug源码看下做了什么操作:
在这里插入图片描述
可以明显看到是加载配置源参数和命令行属性,下个方法configureProfiles是配置当前active的描述文件,这里就不在这里赘述了,感兴趣的可以自己跟下源码走一下。
以上就是configureEnvironment方法的解释,下面我们再回到prepareEnvironment方法
在这里插入图片描述
这个方法是准备系统环境,我们点进去看一下:
在这里插入图片描述
在这里插入图片描述
进到里面有一个重要的监听器,这个监听器就是帮我们加载配置文件的,并将其加入上下文的environment变量中,由下面两图可以清晰的看出,listeners.environmentPrepared这个方法,给我们添加的配置属性都有哪些。
在这里插入图片描述
在这里插入图片描述
我们往下看,看prepareEnvironment方法里的this.bindToSpringApplication((ConfigurableEnvironment)environment);
在这里插入图片描述
这一步主要是将配置文件中的spring.main开头的配置信息绑定到SpringApplication类对应的属性中。
接着往下看:
在这里插入图片描述
ConfigurationPropertySources.attach((Environment)environment);这步操作我没有搞懂,看网上解析是说:“解除前面的迭代器封装”,有大神了解这步的可以在评论区给大家解惑。

这篇博客就是对下图红框内代码的讲解。
在这里插入图片描述

由于源码量过于庞大,这篇博客就先讲解到这里,后续的部分我会分多篇博客来讲解。
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值