上篇博客介绍了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);这步操作我没有搞懂,看网上解析是说:“解除前面的迭代器封装”,有大神了解这步的可以在评论区给大家解惑。
这篇博客就是对下图红框内代码的讲解。