上一节,咱们分析了springboot启动流程中SpringApplication 类的新建过程。知道了其在新建过程当中导入了几个Initializer和Listener,这一节分析一下run方法的执行逻辑。java
public ConfigurableApplicationContext run(String... args) {
//stopwatch主要是一个监控工具,记录启动的耗时。
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//配置java的headless模式
configureHeadlessProperty();
//获取linstener并启动他们
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
//记录启动时输入的参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//准备环境相关配置
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
//打印启动标志
Banner printedBanner = printBanner(environment);
//建立applicationContext
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//准备而且刷新applicationContext
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
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;
}复制代码
配合注释,开始分析run方法的逻辑。spring
StopWatch主要起一个监控的做用,里面的逻辑并不复杂。后面配置了java的headless模式,这个模式和咱们的主流程逻辑影响不大,读者能够自行了解一下。tomcat
在下一行,就到了获取listeners并调用其start方法。这个地方通常人确定会觉得springboot会直接获取在新建时已经获得的listener类。可是 springboot并无这样作,这个地方就体现了我一开始说的,有时候咱们感受很简单的逻辑,springboot想的比咱们更多,因此作的也更复杂。因此关于listener类的调用咱们先不深刻分析,后面结合配置的加载一并分析,在这儿,咱们只须要知道调用了listener类的start方法便可。springboot
在后面一行,使用ApplicationArguments 记录了启动时的参数,而且使用参数配置了环境类environment。environment主要的做用就是记录了咱们在配置文件中配置的那些键值对。因此通过这步以后咱们的配置就已经被读取到项目中了。bash
printBanner方法主要的做用就是打印了启动时的标志。服务器
private Banner printBanner(ConfigurableEnvironment environment) {
if (this.bannerMode == Banner.Mode.OFF) {
return null;
}
ResourceLoader resourceLoader = (this.resourceLoader != null)
? this.resourceLoader : new DefaultResourceLoader(getClassLoader());
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
resourceLoader, this.banner);
if (this.bannerMode == Mode.LOG) {
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
}
//进入打印方法
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
public Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
//获取须要打印的方案
Banner banner = getBanner(environment);
banner.printBanner(environment, sourceClass, out);
return new PrintedBanner(banner, sourceClass);
}复制代码
在getBanner方法中,咱们能够看到springboot是先看看用户有没有自定义,若是有就打印用户自定义的,若是没有就打印默认的,默认就是咱们经常看到的spring那个图案。app
若是须要修改的化,咱们能够直接在resources文件夹下面新建banner.txt文件,里面放置咱们想要的图案好比:框架
****************************************
* * * *
* * * * * *
* * * * * *
* * *
* * * *
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
****************************************复制代码
这样咱们就能够在控制台看到改变了。less
再往下,则开始了对applicationContext类的建立及准备操做。若是读过spring源码的话应该知道对spring框架来讲这是一个极其重要的类。这个类启动完成,就表明spring容器的建立完成了。在springboot中applicationContext还负责了内嵌服务器(好比tomcat之类)的启动工做。因此这部分逻辑,等到后面类加载过程以及内嵌tomcat启动分析的时候,在详细深刻分析。工具
因此到这儿,从顶层看springboot的逻辑已经结束了。这就是我一开始说的,从顶层看springboot的逻辑其实并不复杂。顶层逻辑看完以后,咱们就要开始分析重点了。这个时候挑战才刚刚开始。