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

SpringBoot的启动流程

一.SpringBoot概述

  在 Java 开发领域,Spring Boot 以其 “约定优于配置” 的理念,极大简化了企业级应用的开发与部署。然而,看似简单的java -jar命令背后,隐藏着一套精妙复杂的启动流程。深入理解这个流程,能让我们更好地运用 Spring Boot,提升对框架的认知。

SpringBoot启动流程图:

 

二、启动入口:主类与核心注解剖析

Spring Boot 应用的启动开始于一个标注@SpringBootApplication的主类,如:

 

@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringBootApplication.run(DemoApplication.class, args);}
}

@SpringBootApplication是一个组合注解,由以下三个核心注解构成:

  • @SpringBootConfiguration:本质是@Configuration,表明该类是配置类。在 Spring 容器中,配置类通过@Bean方法定义的 Bean 会被纳入容器管理。例如:
@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyService();}
}

这里myService方法定义的 Bean 会在容器启动时被实例化。

  • @EnableAutoConfiguration:开启自动配置的核心。它通过AutoConfigurationImportSelector类实现。该类的selectImports方法会从META-INF/spring.factories文件中读取所有自动配置类的全限定名。例如,当项目引入spring - jdbc依赖时,DataSourceAutoConfiguration会被加载。在DataSourceAutoConfiguration中,通过@Conditional注解(如@ConditionalOnClass(DataSource.class))判断类路径下是否存在DataSource类,若存在且满足其他条件(如@ConditionalOnMissingBean(DataSource.class)表示容器中不存在该类型 Bean 时),才会配置数据源相关的 Bean。
  • @ComponentScan:默认扫描主类所在包及其子包。它会将标注@Component、@Service、@Repository等注解的类注册到 Spring 容器。例如,一个@Service类:
@Service
public class UserService {// 业务逻辑
}

会被ComponentScan扫描并注册为 Bean。

三、SpringApplication 的初始化细节

(一)实例化 SpringApplication

当调用SpringApplication.run()时,首先创建SpringApplication实例。其构造方法中:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {this.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));// 推断应用类型this.webApplicationType = WebApplicationType.deduceFromClasspath();// 加载并注册初始化器setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));// 加载并注册监听器setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));// 推断主类this.mainApplicationClass = deduceMainApplicationClass();
}

  • 推断应用类型:WebApplicationType.deduceFromClasspath()方法会检查类路径下是否存在org.springframework.web.servlet.DispatcherServlet(Servlet Web 应用)或org.springframework.web.reactive.DispatcherHandler(反应式 Web 应用)。若存在DispatcherServlet,则为WebApplicationType.SERVLET;若存在DispatcherHandler,则为WebApplicationType.REACTIVE;否则为WebApplicationType.NONE。
  • 加载初始化器:通过getSpringFactoriesInstances方法从META-INF/spring.factories文件中读取ApplicationContextInitializer的实现类。例如,org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer会在容器初始化时处理配置警告。
  • 加载监听器:同样从spring.factories加载ApplicationListener实现类,如org.springframework.boot.ClearCachesApplicationListener会在启动时清除缓存。

(二)run 方法解析

run方法是启动的核心,源码如下:

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);// 创建并刷新上下文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);// 启动后处理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;
}

1.准备环境

  • prepareEnvironment方法会构建Environment,加载属性源。属性源的加载顺序为:先加载默认属性(如spring.devtools.add-properties),然后是application.properties(或application.yml),最后是命令行参数。例如,若在application.properties中配置server.port=8081,会覆盖默认的 8080 端口。
  • 处理SpringApplicationRunListener的starting()事件,此时容器尚未创建,但环境已初步配置。

2.创建并刷新上下文

  • createApplicationContext方法根据应用类型创建ApplicationContext。对于 Servlet Web 应用,创建AnnotationConfigServletWebServerApplicationContext;对于反应式 Web 应用,创建AnnotationConfigReactiveWebServerApplicationContext。
  • prepareContext方法会将环境、监听器等与上下文关联,并调用applyInitializers方法执行ApplicationContextInitializer的initialize方法。例如,org.springframework.boot.context.web.ServletWebServerApplicationContextInitializer会在 Servlet Web 应用中初始化 Web 服务器相关配置。
  • refreshContext方法调用context.refresh(),这是 Spring 容器初始化的核心。在refresh方法中:
  1. obtainFreshBeanFactory():创建DefaultListableBeanFactory,加载并解析配置类(包括自动配置类和用户定义的配置类),将 Bean 定义注册到 BeanFactory。
  2. registerBeanPostProcessors(beanFactory):注册后置处理器。其中,ApplicationListenerDetector会检测ApplicationListener类型的 Bean,ConfigurationClassPostProcessor处理@Configuration类,解析其中的@Bean方法和@Import等注解。
  3. finishBeanFactoryInitialization(beanFactory):实例化单例 Bean。对于每个 Bean,先处理@Autowired依赖注入(通过AutowiredAnnotationBeanPostProcessor),再调用@PostConstruct方法(通过CommonAnnotationBeanPostProcessor)进行初始化。例如:

public class MyBean {@Autowiredprivate AnotherBean anotherBean;@PostConstructpublic void init() {// 初始化逻辑}
}
  • 对于 Web 应用,onRefresh方法会启动嵌入式 Web 容器。以 Tomcat 为例,TomcatServletWebServerFactory会创建 Tomcat 实例,配置端口、上下文等,然后启动 Tomcat,监听请求。

3.启动完成处理

  • callRunners方法会调用实现了ApplicationRunner或CommandLineRunner接口的 Bean 的run方法。例如:
@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {// 启动后执行的逻辑,如数据加载}
}
  • 处理SpringApplicationRunListenerstarted()running()事件,标志着应用从启动阶段进入运行阶段。

四、关键扩展点与源码级实现

(一)自定义 Banner 的实现原理

在src/main/resources下创建banner.txt,Spring Boot 启动时会通过BannerService读取并打印。BannerService的printBanner方法会判断是否存在自定义的banner.txt,若存在则读取内容并输出到控制台。如果想自定义 Banner 的颜色等样式,可通过AnsiColor等类进行处理,这在DefaultBanner的printBanner方法中有相关实现。

(二)监听启动事件的底层机制

实现SpringApplicationRunListener或使用@EventListener监听ApplicationEvent,其底层基于 Spring 的事件发布 - 订阅模型。SimpleApplicationEventMulticaster是默认的事件广播器。当调用context.publishEvent时,会遍历所有ApplicationListener,判断是否支持该事件类型(通过supportsEvent方法),若支持则调用onApplicationEvent方法。例如,ApplicationStartedEvent在SpringApplication的started方法中发布:

protected void started(ConfigurableApplicationContext context) {this.applicationContext = context;getRunListeners().started(context);publishEvent(new ApplicationStartedEvent(this, context, args));
}

(三)异常处理与报告的源码逻辑

SpringBootExceptionReporter接口的实现(如DefaultSpringBootExceptionReporter)在handleRunFailure方法中被调用。当启动发生异常时,会收集所有SpringBootExceptionReporter实例,调用它们的reportException方法。DefaultSpringBootExceptionReporter会将异常信息格式化为友好的文本,输出到控制台,包括异常堆栈、自动配置的相关信息(哪些自动配置因条件不满足未生效等),帮助开发者快速定位问题。

五、总结

Spring Boot 的启动流程是一个融合了自动配置、容器初始化、事件驱动和异常处理的复杂系统。从主类注解的解析,到 SpringApplication 的初始化,再到run方法中每一个步骤的源码实现,都体现了框架设计者的精妙构思。在面试中,详细阐述这些细节,如自动配置类的加载机制、refresh方法中 Bean 的初始化过程、事件监听的底层实现等,能充分展示对 Spring Boot 的深度掌握。
掌握 Spring Boot 启动流程的源码级知识,不仅能在开发中更好地优化应用、解决启动问题,更能在技术交流和面试中脱颖而出,成为真正理解框架底层原理的开发者。

相关文章:

  • NoSQL入门实战:MongoDB与Redis核心应用全解析
  • 从 Java 开发到 AI 工程师:全面学习指南
  • 【漫话机器学习系列】238.训练误差与测试误差(Training Error And Test Error)
  • Spring AI 实战:第十一章、Spring AI Agent之知行合一
  • 56认知干货:智能化产业
  • 《政治最后的日子》章节
  • 电动调节 V 型球阀:颗粒状含碱浆液介质的完美解决方案-耀圣
  • 原码、补码、反码、有符号整数、无符号整数
  • 【漫话机器学习系列】239.训练错误率(Training Error Rate)
  • SpringBoot智能排课系统源码开发与实现
  • 正态分布习题集 · 题目篇
  • 2025牛客五一集训派对day4
  • OpenCV入门指南:从环境搭建到第一个图像处理程序
  • 【RocketMQ Broker 相关源码】- broker 启动源码(2)
  • level2.5 函数高阶
  • 气泡图、桑基图的绘制
  • MySQL--索引精通详解
  • TestBench激励与待测
  • 驱动开发硬核特训 · Day 27(上篇):Linux 内核子系统的特性全解析
  • 引入spdlog后程序链接很慢
  • 老人误操作免密支付买几百只鸡崽,经济日报:支付要便捷也要安全
  • 纽约大都会博物馆展“萨金特与巴黎”:从艺术生到明星画家
  • 加拿大总理将赴美同特朗普会晤,重点谈贸易压力
  • 格桑花盛放上海,萨迦艺术团襄阳公园跳起藏族舞
  • 图忆|上海车展40年:中国人的梦中情车有哪些变化(上)
  • 国家卫健委对近日肖某引发舆情问题开展调查