Spring Boot 应用启动流程
2018-10-04 02:29
411 查看
版权声明:Al1en欢迎转载 https://blog.csdn.net/u013310115/article/details/82931539
基于Spring Boot2.0版本,本文主要大致描述启动流程。后面详细介绍流程中各个步骤的具体实现源码。首先我想给大家附上一张启动流程图,后续的文章都会根据图中各个节点展开。
目录
1.SpringApplication的run方法
run方法包含着所有Spring Boot的秘密,它首先开启一个SpringApplicationRunListeners监听器,然后创建应用上下文ApplicationContext,通过上下文加载应用所需要的类和各种环境配置等,最后启动一个应用实例。一路跟进main方法中的run方法会看到如下代码清单。
[code]public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting();//开启监听 try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment);//启动看到的那个大大的Spring Boot,咱们可以自己改。 context = createApplicationContext();//创建应用上下文初始化类(非常重要) exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context);//刷新上下文,其中包括启动内置tomcat等 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; }
2.创建应用上下文初始化类
继续跟进其中的createApplicationContext()方法我们看下是如何创建上下文初始化类,看下面代码清单。
[code]/** * 通过webApplicationType类型有三种可供选择的上下文加载类。 * 1.REACTIVE也就是2.0支持的webflux异步非阻塞请求,理论上适用高并发IO响应的场景。AnnotationConfigReactiveWebServerApplicationContext * 2.SERVLET常用的web应MVC,AnnotationConfigServletWebServerApplicationContext * 3.非web场景上下文加载类AnnotationConfigApplicationContext * webApplicationType类型的设置默认在SpringApplication的构造函数中调用deduceWebApplicationType方法根据类路径中是否存在当前环境的class判断,也可通过手动设置。很尴尬的是手动设置我未曾使用过,但代码中存在设置方法。 */ protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } catch (ClassNotFoundException ex) { throw new IllegalStateException( "Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass", ex); } } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }
3.加载刷新上下文
最终还是回到了Spring 的AbstractApplicationContext中的refresh()方法中。看代码清单如下
[code] protected void refresh(ApplicationContext applicationContext) { Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext); ((AbstractApplicationContext) applicationContext).refresh(); }
4.启动应用
在AbstractApplicationContext的refresh()方法中存在一个函数onRefresh(),如果是web环境在这会选择启动的servlet引擎。如果是普通的web应用而非reactive,则实际的onRefresh方法在AnnotationConfigServletWebServerApplicationContext的父类ServletWebServerApplicationContext中。代码如下
[code]@Override protected void onRefresh() { super.onRefresh(); try { createWebServer();//创建webServer,如tomcat等 } catch (Throwable ex) { throw new ApplicationContextException("Unable to start web server", ex); } }
上面是整个Spring boot2.0的启动加载流程。未详细跟进代码细说,只是大致将整个流程捋一下。具体怎么做为什么这么做会在后面具体开篇章写。文中如有不对之处,欢迎及时指出。共同进步学习是我前进的动力,谢谢。
相关文章推荐
- 【深入SpringBoot 1.3.5 第一章】Boot应用的启动流程
- 【深入SpringBoot 1.3.5 第一章】Boot应用的启动流程
- SpringBoot应用启动流程
- spring Boot 应用通过Docker 来实现构建、运行、发布流程
- SpringBoot应用启动过程分析
- 104. Spring Boot 启动流程分析第三篇【从零开始学Spring Boot】
- 详解Springboot应用启动以及关闭时完成某些操作
- 详解spring boot应用启动原理分析
- jenkins 打包部署springboot应用,后台启动
- 13.5 SpringBoot应用启动错误org.apache.catalina.LifecycleException
- 深入剖析Spring Boot(一)应用搭建和启动
- Spring Boot启动流程详解(一)
- Spring Boot应用首次启动慢的问题
- Spring boot 命令行启动应用
- Spring Boot启动流程详解(一)
- spring boot应用启动原理分析
- spring boot容器启动流程
- spring boot应用启动原理分析
- Spring Boot Web启动流程
- SpringBoot:深入探索内嵌tomcat启动流程