Spring Boot 启动流程详解

本文完整解析 Spring Boot 启动流程的 9 个核心阶段,结合源码与实战场景,帮助开发者深入理解框架运行机制。适用于面试准备和技术原理学习。

一、启动入口:SpringApplication.run()

typescript 复制代码
public static ConfigurableApplicationContext run(Class<?> primarySource, String[] args) {
    return new SpringApplication(primarySource).run(args);
}
AI写代码

二、完整启动流程(9大核心阶段)

阶段1:初始化启动计时器

ini 复制代码
StopWatch stopWatch = new StopWatch();
stopWatch.start();
AI写代码

阶段2:监听器初始化与启动事件

ini 复制代码
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(); // 发布ApplicationStartingEvent
AI写代码
  • ​作用​ :通知所有SpringApplicationRunListener启动开始
  • ​扩展点​:可自定义监听器实现初始化日志、监控等操作

阶段3:环境准备与环境事件

ini 复制代码
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArgs);
AI写代码
css 复制代码
graph TD
    A[创建Environment对象] --> B[加载配置源]
    B --> C[解析Profile配置]
    C --> D[转换配置属性]
    D --> E[发布ApplicationEnvironmentPreparedEvent]
AI写代码

阶段4:打印Banner

ini 复制代码
Banner printedBanner = printBanner(environment);
AI写代码
  • 默认打印SPRING BOOT字符Logo
  • 支持自定义banner.txt文件或完全禁用

阶段5:创建应用上下文

ini 复制代码
context = createApplicationContext();
AI写代码

​​上下文类型判断逻辑:​​

csharp 复制代码
protected ConfigurableApplicationContext createApplicationContext() {
    return switch (this.webApplicationType) {
        case SERVLET -> new AnnotationConfigServletWebServerApplicationContext();
        case REACTIVE -> new AnnotationConfigReactiveWebServerApplicationContext();
        default -> new AnnotationConfigApplicationContext(); // 非Web应用
    };
}
AI写代码

阶段6:上下文预处理

scss 复制代码
prepareContext(context, environment, listeners, appArgs, printedBanner);
AI写代码

​​核心步骤:​​

  1. 关联环境配置到上下文
  2. 应用ApplicationContextInitializer扩展点
  3. 发布ApplicationContextInitializedEvent
  4. 注册主配置类(@SpringBootApplication注解类)
  5. 发布ApplicationPreparedEvent

阶段7:★★★ 核心刷新阶段 ★★★

scss 复制代码
refreshContext(context); // 内部调用Spring的refresh()
AI写代码

​​Spring Framework的12步刷新流程:​​

scss 复制代码
public void refresh() {
    // 1. 准备刷新
    prepareRefresh();
    
    // 2. 获取BeanFactory
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
    // 3. 准备BeanFactory
    prepareBeanFactory(beanFactory);
    
    try {
        // 4. 后置处理BeanFactory
        postProcessBeanFactory(beanFactory);
        
        // 5. ★★★ 执行BeanFactoryPostProcessor(自动配置入口)
        invokeBeanFactoryPostProcessors(beanFactory);
        
        // 6. 注册BeanPostProcessor
        registerBeanPostProcessors(beanFactory);
        
        // 7. 初始化消息源
        initMessageSource();
        
        // 8. 初始化事件广播器
        initApplicationEventMulticaster();
        
        // 9. ★ Web容器启动关键步骤
        onRefresh();
        
        // 10. 注册监听器
        registerListeners();
        
        // 11. ★★★ 实例化所有非延迟单例Bean
        finishBeanFactoryInitialization(beanFactory);
        
        // 12. 完成刷新
        finishRefresh();
    } catch (BeansException ex) {
        // 异常处理...
    }
}
AI写代码
刷新阶段重点说明:
  1. ​​自动装配触发点​ ​(invokeBeanFactoryPostProcessors):

    typescript 复制代码
    protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(
            beanFactory, getBeanFactoryPostProcessors());
        // 这里会处理@SpringBootApplication->加载自动配置类
    }
    AI写代码
  2. ​​嵌入式容器启动点​ ​(onRefresh):

    typescript 复制代码
    protected void onRefresh() {
        super.onRefresh();
        try {
            createWebServer(); // 启动Tomcat/Jetty
        } catch (Throwable ex) {
            throw new ApplicationContextException("Unable to start web server", ex);
        }
    }
    AI写代码
  3. ​​Bean实例化点​ ​(finishBeanFactoryInitialization):

    typescript 复制代码
    protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
        // 初始化所有非延迟单例Bean
        beanFactory.preInstantiateSingletons();
    }
    AI写代码

阶段8:刷新后处理

scss 复制代码
afterRefresh(context, appArgs);
AI写代码
  • ​默认空方法​,开发人员可重写实现自定义逻辑
  • 典型应用:初始化数据库数据、启动定时任务等

阶段9:事件通知与Runner执行

scss 复制代码
stopWatch.stop();
listeners.started(context); // ApplicationStartedEvent
callRunners(context, appArgs); // 执行ApplicationRunner/CommandLineRunner
listeners.running(context); // ApplicationReadyEvent
AI写代码
Runner执行机制:
scss 复制代码
private void callRunners(ApplicationContext context, ApplicationArguments args) {
    List<Object> runners = new ArrayList<>();
    runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
    runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
    AnnotationAwareOrderComparator.sort(runners);
    
    for (Object runner : runners) {
        if (runner instanceof ApplicationRunner) {
            ((ApplicationRunner) runner).run(args);
        }
        if (runner instanceof CommandLineRunner) {
            ((CommandLineRunner) runner).run(args.getSourceArgs());
        }
    }
}
AI写代码

三、Spring Boot启动流程全景图

css 复制代码
graph TD
    A[SpringApplication.run] --> B[启动计时器]
    B --> C[初始化监听器]
    C --> D[发布StartingEvent]
    D --> E[准备Environment]
    E --> F[打印Banner]
    F --> G[创建上下文]
    G --> H[预处理上下文]
    H --> I[refreshContext核心]
    I --> J[后置处理]
    J --> K[发布StartedEvent]
    K --> L[执行Runner]
    L --> M[发布ReadyEvent]
AI写代码

四、关键技术原理

1. 自动装配机制

  1. 通过@EnableAutoConfiguration引入自动配置
  2. AutoConfigurationImportSelector扫描META-INF/spring.factories
  3. 使用@Conditional系列注解实现条件装配

2. 嵌入式容器启动

  1. ​Servle容器​:自动探测并初始化Tomcat/Jetty/Undertow
  2. ​Reactive容器​:支持Netty/Reactor等
  3. ​启动时机​ :在onRefresh()阶段创建WebServer

3. 配置加载顺序

markdown 复制代码
1. 命令行参数
2. 系统环境变量
3. JNDI属性
4. Java系统属性
5. Profile-specific配置(application-{profile}.yml)
6. 主配置文件(application.yml)
7. @Configuration注解的@PropertySource
8. SpringApplication.setDefaultProperties
AI写代码
相关推荐
WeiXin_DZbishe9 小时前
基于springboot宠物领养系统-计算机毕设 附源码【课程设计】94217
vue.js·spring boot·python·django·php·课程设计
架构技术专栏10 小时前
Agent 工具调用:从工具声明到执行反馈
前端·人工智能·后端
vx_bisheyuange10 小时前
基于SpringBoot的体育商品推荐系统
spring boot·毕业设计
码事漫谈11 小时前
你让AI干的活,它可能只干了十分之一——而你根本看不出来
后端
Wx-bishekaifayuan11 小时前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
人工智能·spring boot·后端·python·课程设计·express·旅游
Wx-bishekaifayuan12 小时前
springboot汉中旅游服务平台41442-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
WeiXin_DZbishe12 小时前
基于springboot城市公交实时运营与调度系统-计算机毕设 附源码【课程设计】66173
java·vue.js·spring boot·后端·python·php·课程设计
WeiXin_DZbishe12 小时前
基于Springboot+AI的kuall音乐广场的设计与实现-计算机毕设 附源码【课程设计】12715
vue.js·人工智能·spring boot·后端·python·php·课程设计
A接拉起00712 小时前
2026 年 JavaScript 开发者需要了解的知识
前端·javascript·后端
Bazingga13 小时前
RAG评测体系保姆级教程:生产级实践指南
后端