闲情居|Spring Boot源码解析——Spring Boot系列( 二 )
② SpringApplication的run方法:
主要流程:
第一:创建容器对象
第二:去META-INFO/spring.factories中获取SpringApplicationRunListener监听器(事件发布监听器)
第三:发布容器starting事件(通过spring的事件多播器)
第四:封装命令行参数
第五:准备容器环境
第六:打印Springboot的图标
第七:根据webApplicationType来创建容器
第八:准备容器上下文
第九:发布容器启动事件
第十:发布容器运行事件
【闲情居|Spring Boot源码解析——Spring Boot系列】
public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();//容器对象ConfigurableApplicationContext context = null;Collection exceptionReporters = new ArrayList<>();configureHeadlessProperty();//去META-INFO/spring.factories中获取SpringApplicationRunListener监听器(事件发布监听器)SpringApplicationRunListeners listeners = getRunListeners(args);//发布容器starting事件(通过spring的事件多播器)listeners.starting();try {//封装命令行参数ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);/*** 准备容器环境* 1: 获取或者创建环境* 2:把命令行参数设置到环境中* 3:通过监听器发布环境准备事件*/ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);configureIgnoreBeanInfo(environment);//打印Springboot的图标Banner printedBanner = printBanner(environment);//创建容器根据webApplicationType来创建容器(通过反射创建)context = createApplicationContext();exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);/*** 准备上下文* 1:把环境设置到容器中* 2: 循环调用ApplicationContextInitializer进行容器初始化工作* 3: 发布容器上下文准备完成事件* 4: 注册关于Springboot特性的相关单例Bean* 5: 发布容器上下文加载完毕事件*/prepareContext(context, environment, listeners, applicationArguments, printedBanner);refreshContext(context);//运行ApplicationRunner和CommandLineRunnerafterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}//发布容器启动事件listeners.started(context);//运行ApplicationRunner和CommandLineRunnercallRunners(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;}③ org.springframework.boot.SpringApplication#refreshContext
推荐阅读
- 闲情居|华为云手机发布,真正的5G手机,绕过光刻机的技术壁垒
- 笑看尘世|Spring4基础二二-AOP篇-通知(2)-通知详解
- 闲情居|这个毫无存在感的手机品牌,做了一款可能是今年最奇葩的手机
- 一味宠爱|Springboot整合K8s读取ConfigMap刷新配置
- 闲情居|准备弯道超车?华为放出大招!未来光刻机或将变成废铁?
- 小熊科技|从零学ELK系列(十):SpringBoot接入ELK升级版
- 三好先森|基于JeecgBoot快速开发简历库系统,开源项目,实操分享
- 闲情居|CD机黄金年代的传奇 把玩Revox瑞华士B226 CD机
- 闲情居|突发 | 特斯拉最新事故造成人员伤亡,官方:车辆没有发生故障
- 澄澈的眼|这Spring注入对象处理过程也太细了,收藏了,什么神仙
