闲情居|Spring Boot源码解析——Spring Boot系列( 二 )


闲情居|Spring Boot源码解析——Spring Boot系列② SpringApplication的run方法:
主要流程:
第一:创建容器对象
第二:去META-INFO/spring.factories中获取SpringApplicationRunListener监听器(事件发布监听器)
第三:发布容器starting事件(通过spring的事件多播器)
第四:封装命令行参数
第五:准备容器环境
第六:打印Springboot的图标
第七:根据webApplicationType来创建容器
第八:准备容器上下文
第九:发布容器启动事件
第十:发布容器运行事件
闲情居|Spring Boot源码解析——Spring Boot系列【闲情居|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;}
闲情居|Spring Boot源码解析——Spring Boot系列③ org.springframework.boot.SpringApplication#refreshContext


推荐阅读