当创建完SpringApplication对象之后运行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();
//applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
//回调所有的SpringApplicationRunListener的contextPrepared();
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//刷新容器;ioc容器初始化(如果是web应用还会创建嵌入式的Tomcat)
//扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
refreshContext(context);
//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
//ApplicationRunner先回调,CommandLineRunner再回调
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);
}
//整个SpringBoot应用启动完成以后返回启动的ioc容器;
return context;
}

解析run方法部分代码:

1. prepareContext(context, environment, listeners, applicationArguments, printedBanner);

prepareContext(context, environment, listeners, applicationArguments, printedBanner)

将前面创建的SpringApplication对象中,从类路径下找到META‐INF/spring.factories配置的所有ApplicationContextInitializer的initialize方法

listeners.contextPrepared(context);
回调所有的SpringApplicationRunListener的contextPrepared();

prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded();

7_2.springboot2.x启动配置原理_2.运行run方法的更多相关文章

  1. 7_1.springboot2.x启动配置原理_1.创建SpringApplication对象

    环境准备 springboot2.1.9.idea2019. pom.xml 解析 几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContex ...

  2. 7_3.springboot2.x启动配置原理_3.事件监听机制

    事件监听机制配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListenerioc容器中的 ...

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

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

  4. 七、Spring Boot 启动配置原理

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

  5. Spring boot 启动配置原理

    配置在META-INF/spring.factories 有几个主要的类 ApplicationContextInitializer    创建SpringAplication SpringAppli ...

  6. SpringBoot的启动配置原理

    一.启动流程 创建SpringApplication对象 public class SpringApplication { public SpringApplication(Class... prim ...

  7. 【串线篇】spring boot启动配置原理

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

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

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

  9. SpringBoot 启动配置原理

    几个重要的事件回调机制 ApplicationContextInitializer SpringApplicationRunListener ApplicationRunner CommandLine ...

随机推荐

  1. 【网络】IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解

    楔子: 以Windows系统中IP地址设置界面为参考(如图1), IP地址, 子网掩码, 默认网关 和 DNS服务器, 这些都是什么意思呢? 学习IP地址的相关知识时还会遇到网络地址,广播地址,子网等 ...

  2. delphi DrawText 的用法

    DrawText(hDC: HDC; {设备句柄}lpString: PChar; {文本}nCount: Integer; {要绘制的字符个数; -1 表示全部}var lpRect: TRect; ...

  3. java之短路与&&和短路或||

    短路的意思就是惰性计算,符号右边的就不进行计算了. ||和&&就是这样,

  4. NOIp2018集训test-9-22(am/pm) (联考三day1/day2)

    szzq学长出的题,先orz一下. day1 倾斜的线 做过差不多的题,写在我自己的博客里,我却忘得一干二净,反而李巨记得清清楚楚我写了的. 题目就是要最小化这个东西 $|\frac{y_i-y_j} ...

  5. 高可用开源方案Heartbeat vs Keepalived

    转:http://www.kuqin.com/shuoit/20140623/340745.html 最近因为项目需要,简单的试用了两款高可用开源方案:Keepalived和Heartbeat.两者都 ...

  6. hdu多校第十场 1009 (hdu6699) Block Breaker bfs/模拟

    题意: 紧密排列的方块因为摩擦力一个一个稳定地挤在一起,但当一个方块的四个邻居中,上下两个至少空缺一个,左右两个至少空缺一个,则这个方块也将掉落. 每次锤掉一个方块,求多少个方块受牵连落下. 题解: ...

  7. NtOpenProcess被HOOK,跳回原函数地址后仍然无法看到进程

    点击打开链接http://www.ghoffice.com/bbs/read-htm-tid-103923.html

  8. (转)sql的group by应用

    转载于:http://www.studyofnet.com/news/247.html 本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建 ...

  9. jQuery 基本使用

    index.html <head><meta http-equiv="Content-Type" content="text/html; charset ...

  10. AtCoder ABC 130F Minimum Bounding Box

    题目链接:https://atcoder.jp/contests/abc130/tasks/abc130_f 题目大意 给定地图上 N 个点的坐标和移动方向,它们会以每秒 1 个单位的速度移动,设 A ...