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

二、启动入口:主类与核心注解剖析
Spring Boot 应用的启动开始于一个标注**@SpringBootApplication**的主类,如:
java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringBootApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication是一个组合注解,由以下三个核心注解构成:
- @SpringBootConfiguration:本质是@Configuration,表明该类是配置类。在 Spring 容器中,配置类通过@Bean方法定义的 Bean 会被纳入容器管理。例如:
java
@Configuration
public class AppConfig {
@Bean
public 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类:
java
@Service
public class UserService {
// 业务逻辑
}
会被**ComponentScan
**扫描并注册为 Bean。
三、SpringApplication 的初始化细节
(一)实例化 SpringApplication
当调用SpringApplication.run()时,首先创建SpringApplication实例。其构造方法中:
java
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方法是启动的核心,源码如下:
java
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方法中:
- obtainFreshBeanFactory():创建DefaultListableBeanFactory,加载并解析配置类(包括自动配置类和用户定义的配置类),将 Bean 定义注册到 BeanFactory。
- registerBeanPostProcessors(beanFactory):注册后置处理器。其中,ApplicationListenerDetector会检测ApplicationListener类型的 Bean,ConfigurationClassPostProcessor处理@Configuration类,解析其中的@Bean方法和@Import等注解。
- finishBeanFactoryInitialization(beanFactory):实例化单例 Bean。对于每个 Bean,先处理@Autowired依赖注入(通过AutowiredAnnotationBeanPostProcessor),再调用@PostConstruct方法(通过CommonAnnotationBeanPostProcessor)进行初始化。例如:
java
public class MyBean {
@Autowired
private AnotherBean anotherBean;
@PostConstruct
public void init() {
// 初始化逻辑
}
}
- 对于 Web 应用,
onRefresh
方法会启动嵌入式 Web 容器。以 Tomcat 为例,**TomcatServletWebServerFactory
**会创建 Tomcat 实例,配置端口、上下文等,然后启动 Tomcat,监听请求。
3.启动完成处理
- callRunners方法会调用实现了ApplicationRunner或CommandLineRunner接口的 Bean 的run方法。例如:
java
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 启动后执行的逻辑,如数据加载
}
}
- 处理**
SpringApplicationRunListener
** 的**started()
** 和**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方法中发布:
java
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 启动流程的源码级知识,不仅能在开发中更好地优化应用、解决启动问题,更能在技术交流和面试中脱颖而出,成为真正理解框架底层原理的开发者。