当前位置: 首页 > news >正文

Spring Boot启动流程详解

Spring Boot是一个基于Spring框架的快速开发工具,它可以帮助我们快速搭建一个可运行的Spring应用。本文将详细介绍Spring Boot的启动流程,帮助大家更好地理解Spring Boot的工作原理。

一、Spring Boot启动流程概述

Spring Boot的启动流程可以分为以下几个阶段:

  • 初始化配置

  • 创建应用程序上下文

  • 刷新上下文(启动核心)

  • 通知监听者-启动程序完成

public ConfigurableApplicationContext run(String... args) {long startTime = System.nanoTime();DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();ConfigurableApplicationContext context = null;this.configureHeadlessProperty();SpringApplicationRunListeners listeners = this.getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {// 获取args参数对象ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 读取Springboot配置文件并创建Environment对象// 这里创建的Environment对象实际为ConfigurableEnvironmentConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);this.configureIgnoreBeanInfo(environment);// 打印Banner图标Banner printedBanner = this.printBanner(environment);// 创建ApplicationContext应用行下文,即创建容器context = this.createApplicationContext();context.setApplicationStartup(this.applicationStartup);// 准备容器this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);// 初始化容器this.refreshContext(context);this.afterRefresh(context, applicationArguments);Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);}// 调用运行时监听器的started()方法// 该方法需要在应用程序启动后,CommandLineRunners和ApplicationRunners被调用前执行listeners.started(context, timeTakenToStartup);this.callRunners(context, applicationArguments);} catch (Throwable var12) {this.handleRunFailure(context, var12, listeners);throw new IllegalStateException(var12);}try {Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);listeners.ready(context, timeTakenToReady);return context;} catch (Throwable var11) {this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);throw new IllegalStateException(var11);}}

这段代码是Spring Boot启动时的核心方法,它的主要作用是创建和配置Spring应用程序的上下文。

首先,记录启动开始时间,并创建一个DefaultBootstrapContext对象和一个空的ConfigurableApplicationContext对象。然后,获取运行监听器并调用其starting()方法,表示应用程序正在启动。

接下来,创建一个ApplicationArguments对象,用于存储应用程序的命令行参数。然后,调用prepareEnvironment()方法准备环境,包括加载配置文件、扫描并注册Bean、初始化Spring容器等。

接着,打印应用程序的Banner信息,并创建一个新的ConfigurableApplicationContext对象。设置应用程序的启动类和启动配置,并调用prepareContext()方法准备上下文。

然后,调用refreshContext()方法刷新上下文,并调用afterRefresh()方法执行一些后续操作。计算应用程序启动所花费的时间,并在日志中记录启动信息。

最后,调用运行监听器的started()方法,表示应用程序已经启动。然后,调用callRunners()方法执行所有的CommandLineRunner和ApplicationRunner接口实现。

如果在启动过程中出现异常,会调用handleRunFailure()方法处理异常,并抛出一个IllegalStateException异常。

最后,调用运行监听器的ready()方法,表示应用程序已经准备好,并返回创建的ConfigurableApplicationContext对象。

二、详细解析

初始化配置

Spring Boot启动时,会首先加载配置文件(如application.properties或application.yml)中的配置信息。这些配置信息包括数据源、缓存、日志等相关信息。

 private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {ConfigurableEnvironment environment = this.getOrCreateEnvironment();this.configureEnvironment(environment, applicationArguments.getSourceArgs());ConfigurationPropertySources.attach(environment);listeners.environmentPrepared(bootstrapContext, environment);DefaultPropertiesPropertySource.moveToEnd(environment);Assert.state(!environment.containsProperty("spring.main.environment-prefix"), "Environment prefix cannot be set via properties.");this.bindToSpringApplication(environment);if (!this.isCustomEnvironment) {EnvironmentConverter environmentConverter = new EnvironmentConverter(this.getClassLoader());environment = environmentConverter.convertEnvironmentIfNecessary(environment, this.deduceEnvironmentClass());}ConfigurationPropertySources.attach(environment);return environment;}

这段代码是一个名为prepareEnvironment的方法,它接收三个参数:listeners(SpringApplicationRunListeners类型)、bootstrapContext(DefaultBootstrapContext类型)和applicationArguments(ApplicationArguments类型)。该方法的主要作用是准备和配置Spring应用程序的环境。

首先,通过调用this.getOrCreateEnvironment()方法获取或创建一个ConfigurableEnvironment对象,并将其赋值给变量environment。

接下来,调用this.configureEnvironment(environment, applicationArguments.getSourceArgs())方法对环境进行配置,传入environment和applicationArguments.getSourceArgs()作为参数。

然后,调用ConfigurationPropertySources.attach(environment)方法将环境附加到配置属性源。

接着,调用listeners.environmentPrepared(bootstrapContext, environment)方法通知监听器环境已准备好。

然后,调用DefaultPropertiesPropertySource.moveToEnd(environment)方法将默认属性源移动到环境的末尾。

接下来,使用断言语句Assert.state(!environment.containsProperty(“spring.main.environment-prefix”), “Environment prefix cannot be set via properties.”)确保环境中不包含名为"spring.main.environment-prefix"的属性。

然后,调用this.bindToSpringApplication(environment)方法将环境绑定到Spring应用程序。

如果this.isCustomEnvironment为false,则创建一个EnvironmentConverter对象,并调用其convertEnvironmentIfNecessary(environment, this.deduceEnvironmentClass())方法根据需要转换环境。

最后,再次调用ConfigurationPropertySources.attach(environment)方法将环境附加到配置属性源,并将environment返回。

创建应用程序上下文

初始化和配置好后,开始创建应用程序上下文,createApplicationContext ,关键的工厂BeanFactory就是此处创建

   protected ConfigurableApplicationContext createApplicationContext() {return this.applicationContextFactory.create(this.webApplicationType);}

刷新上下文(启动核心)

工厂配置,bean处理器配置,类的扫描,解析,bean定义,bean类信息缓存,服务器创建,bean实例化,动态代理对象的创建等,

 private void refreshContext(ConfigurableApplicationContext context) {if (this.registerShutdownHook) {shutdownHook.registerApplicationContext(context);}this.refresh(context);}

该方法的主要作用是刷新Spring应用程序的上下文。

首先,如果this.registerShutdownHook为true,则调用shutdownHook.registerApplicationContext(context)方法将应用程序上下文注册到关闭钩子中。

然后,调用this.refresh(context)方法刷新上下文。

 public void refresh() throws BeansException, IllegalStateException {synchronized(this.startupShutdownMonitor) {StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");this.prepareRefresh();ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();this.prepareBeanFactory(beanFactory);try {this.postProcessBeanFactory(beanFactory);StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");//注册并实例化bean工厂处理器,并调用他们this.invokeBeanFactoryPostProcessors(beanFactory);//注册并实例化bean处理器this.registerBeanPostProcessors(beanFactory);beanPostProcess.end();this.initMessageSource();this.initApplicationEventMulticaster();//初始化一些与上下文有特别关系的bean对象(创建tomcat)this.onRefresh();this.registerListeners();// 实例化所有bean工厂缓存的bean对象(剩下的).this.finishBeanFactoryInitialization(beanFactory);//发布通知-通知上下文刷新完成(包括启动tomcat)this.finishRefresh();} catch (BeansException var10) {if (this.logger.isWarnEnabled()) {this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10);}this.destroyBeans();this.cancelRefresh(var10);throw var10;} finally {this.resetCommonCaches();contextRefresh.end();}}}

该方法的主要作用是刷新Spring应用程序的上下文。

首先,通过同步块来确保线程安全。然后,创建一个名为contextRefresh的StartupStep对象,并调用this.prepareRefresh()方法准备刷新。接着,获取一个新的BeanFactory对象,并调用this.prepareBeanFactory(beanFactory)方法准备BeanFactory。

接下来,在try-catch语句中执行以下操作:

  • 调用this.postProcessBeanFactory(beanFactory)方法对BeanFactory进行后处理。
  • 创建一个名为beanPostProcess的StartupStep对象,并调用this.invokeBeanFactoryPostProcessors(beanFactory)方法执行BeanFactory的后处理器。
  • 调用this.registerBeanPostProcessors(beanFactory)方法注册Bean的后处理器。
  • 结束beanPostProcess的计时。
  • 调用this.initMessageSource()方法初始化消息源。
  • 调用this.initApplicationEventMulticaster()方法初始化应用程序事件多播器。
  • 调用this.onRefresh()方法执行刷新操作。
  • 调用this.registerListeners()方法注册监听器。
  • 调用this.finishBeanFactoryInitialization(beanFactory)方法完成BeanFactory的初始化。
  • 调用this.finishRefresh()方法完成刷新操作。

如果在执行过程中出现BeansException异常,会记录警告日志,销毁Beans,取消刷新操作,并抛出该异常。最后,在finally语句中重置缓存和计时器。

通知监听者-启动程序完成

发布通知监听器启动完成,监听器会根据事件类型做个性化操作

  void starting(ConfigurableBootstrapContext bootstrapContext, Class<?> mainApplicationClass) {this.doWithListeners("spring.boot.application.starting", (listener) -> {listener.starting(bootstrapContext);}, (step) -> {if (mainApplicationClass != null) {step.tag("mainApplicationClass", mainApplicationClass.getName());}});}void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {this.doWithListeners("spring.boot.application.environment-prepared", (listener) -> {listener.environmentPrepared(bootstrapContext, environment);});}void contextPrepared(ConfigurableApplicationContext context) {this.doWithListeners("spring.boot.application.context-prepared", (listener) -> {listener.contextPrepared(context);});}void contextLoaded(ConfigurableApplicationContext context) {this.doWithListeners("spring.boot.application.context-loaded", (listener) -> {listener.contextLoaded(context);});}void started(ConfigurableApplicationContext context, Duration timeTaken) {this.doWithListeners("spring.boot.application.started", (listener) -> {listener.started(context, timeTaken);});}void ready(ConfigurableApplicationContext context, Duration timeTaken) {this.doWithListeners("spring.boot.application.ready", (listener) -> {listener.ready(context, timeTaken);});}void failed(ConfigurableApplicationContext context, Throwable exception) {this.doWithListeners("spring.boot.application.failed", (listener) -> {this.callFailedListener(listener, context, exception);}, (step) -> {step.tag("exception", exception.getClass().toString());step.tag("message", exception.getMessage());});}private void callFailedListener(SpringApplicationRunListener listener, ConfigurableApplicationContext context, Throwable exception) {try {listener.failed(context, exception);} catch (Throwable var6) {if (exception == null) {ReflectionUtils.rethrowRuntimeException(var6);}if (this.log.isDebugEnabled()) {this.log.error("Error handling failed", var6);} else {String message = var6.getMessage();message = message != null ? message : "no error message";this.log.warn("Error handling failed (" + message + ")");}}}private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction) {this.doWithListeners(stepName, listenerAction, (Consumer)null);}private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction, Consumer<StartupStep> stepAction) {StartupStep step = this.applicationStartup.start(stepName);this.listeners.forEach(listenerAction);if (stepAction != null) {stepAction.accept(step);}step.end();}

SpringApplicationRunListeners是Spring Boot框架中的一个接口,用于监听应用程序的运行过程。它提供了一系列的回调方法,可以在应用程序的不同阶段执行自定义的逻辑。

以下是SpringApplicationRunListeners接口中定义的一些主要方法:

  • starting(): 在应用程序启动之前调用,可以执行一些初始化操作。
  • environmentPrepared(): 在环境准备完成后调用,可以获取到应用程序的环境信息。
  • contextPrepared(): 在上下文准备完成后调用,可以获取到应用程序的上下文对象。
  • contextLoaded(): 在上下文加载完成后调用,可以执行一些额外的配置操作。
  • started(): 在应用程序启动完成后调用,可以执行一些后续处理操作。
  • running(): 在应用程序运行期间持续调用,可以执行一些周期性的任务。
  • failed(): 在应用程序启动失败时调用,可以执行一些错误处理操作。
  • stopped(): 在应用程序停止后调用,可以执行一些清理操作。

总结

通过以上介绍,我们了解了Spring Boot的启动流程。Spring Boot通过自动化的配置和简化的部署流程,使得我们能够快速搭建一个可运行的Spring应用。希望本文能够帮助大家更好地理解Spring Boot的工作原理。

相关文章:

  • Java后端 - 一面凉经 - 得物(国际电商)
  • 安装sqlserver2022最新版只能使用.\SQLEXPRESS登录数据库怎么修改成.
  • Java实战:Spring Boot整合Canal与RabbitMQ实时监听数据库变更并高效处理
  • 2023年第三届中国高校大数据挑战赛(第二场)A题思路
  • EVE-NG桥接虚拟网卡实现与虚拟机通讯
  • RESTful接口规范参考
  • vue element plus Avatar 头像
  • 扼杀网络中的环路:STP、RSTP、MSTP
  • 万界星空科技MES系统中的车间管理的作用
  • 矩阵键盘中为什么有键位并联二极管?
  • Python光速入门 - Flask轻量级框架
  • Kafka 设计之消息传递保障
  • Web开发介绍,制作小网站流程和需要的技术【详解】
  • 影响哈默纳科Harmonic减速机使用寿命的5大因素
  • 【手游联运平台搭建】游戏平台的作用
  • 【剑指offer】让抽象问题具体化
  • 【刷算法】求1+2+3+...+n
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • C++入门教程(10):for 语句
  • Spring Boot快速入门(一):Hello Spring Boot
  • spring学习第二天
  • Vue ES6 Jade Scss Webpack Gulp
  • Web Storage相关
  • Webpack 4x 之路 ( 四 )
  • 读懂package.json -- 依赖管理
  • 翻译--Thinking in React
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 如何解决微信端直接跳WAP端
  • 时间复杂度与空间复杂度分析
  • 系统认识JavaScript正则表达式
  • 学习Vue.js的五个小例子
  • 译有关态射的一切
  • raise 与 raise ... from 的区别
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • # .NET Framework中使用命名管道进行进程间通信
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (06)Hive——正则表达式
  • (12)Hive调优——count distinct去重优化
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (vue)页面文件上传获取:action地址
  • (算法)求1到1亿间的质数或素数
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .NET Core引入性能分析引导优化
  • .Net Redis的秒杀Dome和异步执行
  • .NET 中选择合适的文件打开模式(CreateNew, Create, Open, OpenOrCreate, Truncate, Append)
  • .NET/C# 使用反射注册事件
  • .net访问oracle数据库性能问题
  • @data注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • @Import注解详解
  • @staticmethod和@classmethod的作用与区别
  • [].slice.call()将类数组转化为真正的数组
  • [04] Android逐帧动画(一)
  • [github配置] 远程访问仓库以及问题解决
  • [IE9] IE9 beta版下载链接