前言
如果非说Spring Boot微框架提供了点儿本人特有的货色,,在外围类层面,也就是SpringApplication了。它提供了Spring Boot程序启动的一站式解决方案。在没有非凡需要的状况下,默认模板化后的执行流程就能够满足需要了;但有非凡需要也没关系,SpringApplication在适合的流程结点凋谢了一系列不同类型的扩大点,咱们能够通过这些扩大点对SpringBoot程序的启动和敞开过程进行扩大。
注释
上一篇文章《Spring Boot外围运行原理神秘的面纱!》通过剖析SpringApplication类实例化的代码咱们晓得在此过程中实现了根本的配置文件的加载和实例化。当SpringApplication对象创立之后,通过调用run办法来进行Spring Boot的启动和运行。
run办法外围流程
咱们通过约定run办法的源代码对该办法的次要流程大体能够归纳如下:
- 获取监听器和参数配置
- 打印Banner
- 创立并初始化容器
-
监听器发送告诉
/** * 运行Spring利用,创立并刷新一个新的IOC容器并返回 */ public ConfigurableApplicationContext run(String... args) { //创立一个用于统计run办法启动时长的对象 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 Banner printedBanner = printBanner(environment); //创立容器 context = createApplicationContext(); //获取异样信息 exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); //筹备容器 prepareContext(context, environment, listeners, applicationArguments, printedBanner); //初始化容器 refreshContext(context); //初始化后操作 afterRefresh(context, applicationArguments); //进行时长统计 stopWatch.stop(); //打印启动日志 if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } //告诉监听器,容器启动实现 listeners.started(context); //调用ApplicationRunner和CommandLineRunner办法 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;
当然,除了外围操作,run办法运行过程中还波及到了启动时长统计、异样、日志等辅助操作。
深刻摸索执行流程
Spirng Boot启动过程重点在于事件监听、初始化环境、容器创立及初始化操作。大体归纳如下:
- 收集并加载各种可用的条件和回调接口,例如:ApplicationContextInitializer和的ApplicationListener等。调用它们的started()办法,告诉它们:“Spring Boot利用要开始执行了”。
- 创立并筹备所有的Environment(配置属性),遍历调用所有SpringApplicationRunListener的environmentPrepared()的办法,告诉它们:“以后SpringBoot利用应用的Environment筹备好了”。
- 创立并初始化ApplicationContext,例如:设置Environment,加载配置等。遍历调用所有SpringApplicationRunListener的contextPrepared()办法,告诉它们:“SpringBoot利用应用的ApplicationContext筹备好了”。)遍历调用所有SpringApplicationRunListener的contextLoaded()办法,告知所有SpringApplicationRunListener, ApplicationContext”装填结束”。
- 调用ApplicationContext的refresh()办法,实现IoC容器可用的最初一道工序。注册并执行CommandLineRunner。)失常状况下,遍历执行SpringApplicationRunListener的finished()办法,告知它们:“OK!”。
至此,一个残缺的SpringBoot利用启动结束!
总结
至此,咱们对SpringBoot的外围组件实现了根本的分析,综合来看,大部分的货色都是Spring框架背地原有的一些概念和实际形式,SpringBoot只是在这些概念和实际形式上对特定的场景实现进行了固化和升华,而也恰好是这些固化让咱们开发基于Spring框架的利用更加不便高效。
最初的最初
为初学者提供学习指南,为从业者提供参考价值。我深信码农也具备产生洞见的能力。扫描下图二维码关注,学习和交换!
发表回复