11

SpringBoot 启动分为初始化、刷新容器、启动完成三大阶段,核心高阶流程:

  1. SpringApplication 实例化阶段

1. 推断应用类型(REACTIVE/SERVLET/NONE):通过判断类路径下是否存在 Servlet / DispatcherHandler 类;
​
2. 加载ApplicationContextInitializer与ApplicationListener:通过SPI机制加载SPI接口实现类;
​
3. 推断主类:通过堆栈轨迹获取启动类,作为SpringBoot主配置类。

  1. run() 方法核心执行流程

1. 启动计时器、获取SpringApplicationRunListeners(事件广播器);
​
2. 准备环境(ConfigurableEnvironment):加载配置文件(application.yml/bootstrap.yml)、系统属性、环境变量,完成配置解析;
​
3. 创建ApplicationContext:根据应用类型创建 AnnotationConfigServletWebServerApplicationContext ;
​
4. 刷新容器:调用 AbstractApplicationContext.refresh() ,完成Bean的加载、实例化、初始化、AOP代理;
​
5. 启动Web服务器:Tomcat/Jetty/Undertow通过 ServletWebServerFactory 自动创建并启动;
​
6. 执行Runner:调用 ApplicationRunner 与 CommandLineRunner ,完成启动后初始化逻辑。

posted @ 2026-02-21 23:30  111阳  阅读(19)  评论(0)    收藏  举报