SpringBoot 完整启动原理
SpringBoot 启动核心分为两大块主类SpringBootApplication注解组合能力SpringApplication.run()完整启动流程包含环境准备、上下文创建、自动配置、Bean 扫描、内嵌容器启动等步骤。下面分层梳理逻辑清晰、贴合面试答题节奏。一、先讲核心注解 SpringBootApplication该注解是复合注解等价三个注解合并Configuration EnableAutoConfiguration ComponentScanConfiguration标记当前类为配置类允许内部Bean定义对象交给 Spring 容器管理。ComponentScan默认扫描当前主类所在包及其所有子包下带Controller/Service/Repository/Component的类自动注册 Bean 到 IOC 容器。EnableAutoConfigurationSpringBoot 核心开启自动装配 底层通过Import(AutoConfigurationImportSelector)读取META-INF/spring.factories文件中预定义的自动配置类 根据项目引入的依赖 jar、条件注解ConditionalOnClass、ConditionalOnMissingBean按需自动创建 Bean无需手动 XML 配置。补充还有AutoConfigurationPackage把主包注册为自动配置扫描根路径。二、主入口SpringApplication.run (主类.class, args)run 方法分为两大阶段实例化 SpringApplication 对象执行 run 启动流程阶段 1new SpringApplication () 实例化做 4 件初始化工作记录主启动类sources推断应用类型存在 Servlet 相关类 → WEB 应用内嵌 Tomcat/Jetty/Undertow无 Web 依赖 → 普通标准 Java 应用加载META-INF/spring.factories中两类扩展点ApplicationContextInitializer容器上下文初始化器ApplicationListener全局事件监听器设置资源加载器、主类推断标记。阶段 2run () 核心 10 步启动流程面试核心流程public ConfigurableApplicationContext run(String... args) { // 1. 创建StopWatch记录启动耗时 StopWatch stopWatch new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context null; CollectionSpringBootExceptionReporter exceptionReporters; // 2. 设置系统属性开启java.awt.headless无头模式 configureHeadlessProperty(); // 3. 获取并启动SpringApplicationRunListeners启动监听器 SpringApplicationRunListeners listeners getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments new DefaultApplicationArguments(args); // 4. 准备环境加载配置文件yml/properties、命令行参数、系统环境变量 ConfigurableEnvironment environment prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); // 打印Banner启动图案 Banner printedBanner printBanner(environment); // 5. 根据应用类型创建对应ApplicationContext容器 context createApplicationContext(); // 6. 执行ApplicationContextInitializer初始化上下文 prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 7. 刷新容器核心Spring IOC完整初始化流程 refreshContext(context); // 8. 后置处理容器扩展逻辑 afterRefresh(context, applicationArguments); stopWatch.stop(); // 打印启动耗时日志 if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } // 9. 发布容器启动完成事件 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; }逐段提炼关键步骤启动监听器 starting发布 ApplicationStartingEvent项目刚启动还未加载环境。准备环境 prepareEnvironment加载多来源配置优先级命令行参数 系统环境变量 application.yml/application.properties 默认配置 发布 ApplicationEnvironmentPreparedEvent。创建上下文 createApplicationContextweb 环境创建AnnotationConfigServletWebServerApplicationContext普通应用创建AnnotationConfigApplicationContext。上下文前置准备 prepareContext绑定环境到容器、注册主启动类为配置类、执行所有 ApplicationContextInitializer发布 ApplicationContextInitializedEvent。refreshContext () 刷新 IOC 容器最核心内部执行 Spring 原生 refresh () 整套流程加载所有配置类组件扫描 ComponentScan注册业务 Bean 定义执行自动装配加载 META-INF/spring.factories 自动配置类BeanFactory 后置处理、Bean 定义解析实例化单例 Bean、依赖注入、初始化PostConstruct、InitializingBeanWEB 环境下创建内嵌 Tomcat 容器并启动绑定端口监听请求。afterRefresh 容器后置扩展空方法可自定义重写做扩展。发布 started 事件 执行启动 Runner发布 ApplicationStartedEvent 执行ApplicationRunner/CommandLineRunner适合项目启动后加载缓存、初始化数据。发布 running 事件发布 ApplicationReadyEvent代表服务完全就绪可以接收请求。三、关键核心机制补充面试加分1. 自动装配原理完整闭环EnableAutoConfiguration导入AutoConfigurationImportSelector从 classpath 下META-INF/spring.factories读取 key 为EnableAutoConfiguration的所有配置类全限定名通过条件注解ConditionalOnXXX判断导入了对应依赖才生效没有对应 Bean 才自动创建配置类内部通过Bean自动装配如 DataSource、RedisTemplate、DispatcherServlet 等组件。2. 内嵌 Web 容器启动逻辑如果是 Web 应用refresh () 流程中会触发ServletWebServerApplicationContext的 onRefresh 方法自动探测 classpath 存在的 web 容器Tomcat 默认创建内嵌 Server 工厂生成 Tomcat 实例注册 SpringMVC 的 DispatcherServlet 到 Tomcat启动容器绑定 server.port 端口接收 HTTP 请求。 SpringBoot 无需外部 Tomcat打包 jar 可直接运行根源就在这里。3. 事件监听机制观察者模式全流程通过事件驱动Starting → EnvironmentPrepared → ContextInitialized → Started → Ready 开发者可自定义 ApplicationListener 监听对应事件做扩展。四、精简口述总结版SpringBoot 启动分为注解基础和 run 执行流程主类SpringBootApplication整合配置、包扫描、自动装配三大能力SpringApplication.run 分为实例初始化、环境加载、容器创建、刷新 IOC、启动内嵌容器、执行启动 Runner核心 refresh 方法完成 Bean 扫描、自动配置加载、Bean 实例化依赖注入自动装配读取 spring.factories 文件依靠条件注解按需创建组件Web 环境自动启动内嵌 Tomcat整套流程零 XML简化 Spring 繁琐配置。