复制
刷新Spring应用上下文然后我们回到SpringApplication#run
方法中的this.refreshContext(context);
这行代码 , 并进入方法体
private void refreshContext(ConfigurableApplicationContext context) {// 刷新应用上下文this.refresh(context);if (this.registerShutdownHook) {try {// 如果需要注册关闭钩子 , 则应用上下文注册关闭钩子context.registerShutdownHook();} catch (AccessControlException var3) {}}}protected void refresh(ApplicationContext applicationContext) {Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);((AbstractApplicationContext)applicationContext).refresh();}
复制
AbstractApplicationContext#refresh方法public void refresh() throws BeansException, IllegalStateException {// 刷新应用上下文过程使用了监视器锁synchronized(this.startupShutdownMonitor) {// 预刷新this.prepareRefresh();// 获取刷新beanFactoryConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();// 预备beanFactorythis.prepareBeanFactory(beanFactory);try {// 后置处理beanFactorythis.postProcessBeanFactory(beanFactory);// 执行beanFactory后置处理器this.invokeBeanFactoryPostProcessors(beanFactory);// 注册bean后置处理器this.registerBeanPostProcessors(beanFactory);// 初始化消息源this.initMessageSource();// 初始化应用事件广播this.initApplicationEventMulticaster();// 调用onRefres方法,如果是Servlet应用程序,这个方法会去创建web服务器this.onRefresh();// 注册监听器this.registerListeners();// 结束beanFactory初始化this.finishBeanFactoryInitialization(beanFactory);// 结束刷新,如果是Servlet应用程序 , 个方法会去启动web服务器this.finishRefresh();} catch (BeansException var9) {if (this.logger.isWarnEnabled()) {this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);}// 发生异常时会销毁bean并取消刷新this.destroyBeans();this.cancelRefresh(var9);throw var9;} finally {// finally语句块中重新设置公共缓存this.resetCommonCaches();}}}
复制
ServletWebServerApplicationContext#onRefresh方法protected void onRefresh() {super.onRefresh();try {this.createWebServer();} catch (Throwable var2) {throw new ApplicationContextException("Unable to start web server", var2);}}
复制
ServletWebServerApplicationContext#createWebServer方法private void createWebServer() {WebServer webServer = this.webServer;ServletContext servletContext = this.getServletContext();if (webServer == null && servletContext == null) {ServletWebServerFactory factory = this.getWebServerFactory();this.webServer = factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});} else if (servletContext != null) {try {this.getSelfInitializer().onStartup(servletContext);} catch (ServletException var4) {throw new ApplicationContextException("Cannot initialize servlet context", var4);}}this.initPropertySources();}
复制
Spring容器启动后运行任务SpringApplication#callRunners方法private void callRunners(ApplicationContext context, ApplicationArguments args) {List<Object> runners = new ArrayList();// 通过应用上下文获取所有ApplicationRunner接口实现类的bean集合runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());// 通过应用上下文获取所有CommandLineRunner接口实现类的bean集合runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());AnnotationAwareOrderComparator.sort(runners);Iterator var4 = (new LinkedHashSet(runners)).iterator();// 遍历执行ApplicationRunner实现类和CommandLineRunner实现类中的run方法while(var4.hasNext()) {Object runner = var4.next();if (runner instanceof ApplicationRunner) {this.callRunner((ApplicationRunner)runner, args);}if (runner instanceof CommandLineRunner) {this.callRunner((CommandLineRunner)runner, args);}}private void callRunner(ApplicationRunner runner, ApplicationArguments args) {try {runner.run(args);} catch (Exception var4) {throw new IllegalStateException("Failed to execute ApplicationRunner", var4);}}private void callRunner(CommandLineRunner runner, ApplicationArguments args) {try {runner.run(args.getSourceArgs());} catch (Exception var4) {throw new IllegalStateException("Failed to execute CommandLineRunner", var4);}}
复制
处理启动异常最后我们来看看SprignBoot
应用程序启动发生异常时调用的方法
SpringApplication#handleRunFailure方法 private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception, Collection<SpringBootExceptionReporter> exceptionReporters, SpringApplicationRunListeners listeners) {try {try {// 处理程序退出编码this.handleExitCode(context, exception);if (listeners != null) {// 应用启动监听器发布启动失败事件listeners.failed(context, exception);}} finally {// 报告异常this.reportFailure(exceptionReporters, exception);if (context != null) {// 关闭Spring IOC容器context.close();}}} catch (Exception var9) {logger.warn("Unable to close ApplicationContext", var9);}// 调用反射工具类抛出运行时异常ReflectionUtils.rethrowRuntimeException(exception);}
推荐阅读
- 一次SpringBoot版本升级,引发的血案
- SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器
- SpringBoot 02: 初识SpringBoot
- 超详细 SpringBoot 整合 Elasticsearch .md
- SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource
- MindStudio模型训练场景精度比对全流程和结果分析
- 纸嫁衣4第三章交错通关流程图文攻略
- 纸嫁衣4第一章异途通关流程图文攻略-纸嫁衣4红丝缠第一章怎么过
- 纸嫁衣4第二章不期通关流程图文攻略-纸嫁衣4红丝缠第二章怎么过
- 洛克王国初秋落叶活动流程攻略-洛克王国初秋落叶怎么玩