SpringBoot 的启动核心是通过SpringApplication.run()方法完成,其本质是对 Spring 框架的封装与扩展,实现了 "自动配置"、"内嵌容器" 等核心特性。下面结合源码分析其启动过程:
一、启动入口:SpringApplication.run()
SpringBoot 应用的启动入口是主类的main方法,核心调用SpringApplication.run(主类.class, args),例如:
java运行
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args); // 启动核心
}
}
SpringApplication.run()是一个静态方法,内部会先创建SpringApplication实例,再调用其run(args)方法,源码简化如下:
java运行
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class<?>[] { primarySource }, args);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
// 1. 创建SpringApplication实例 2. 调用实例的run方法
return new SpringApplication(primarySources).run(args);
}
二、SpringApplication实例初始化
SpringApplication的构造方法会完成初始化工作,核心是确定应用类型、加载初始化器、加载监听器,源码关键逻辑如下:
java运行
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 1. 确定应用类型(Servlet/Reactive/普通)
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 2. 加载初始化器(ApplicationContextInitializer)
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 3. 加载监听器(ApplicationListener)
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 4. 确定主类(从main方法所在类推断)
this.mainApplicationClass = deduceMainApplicationClass();
}
关键步骤解析:
-
确定应用类型:
WebApplicationType.deduceFromClasspath()通过判断 classpath 中是否存在特定类(如Servlet、ReactiveWebServerFactory),确定应用是:SERVLET(传统 Web 应用,依赖 Servlet API)REACTIVE(响应式 Web 应用,依赖 Spring WebFlux)NONE(非 Web 应用)
-
加载初始化器和监听器:核心是
getSpringFactoriesInstances()方法,通过Spring 的 SPI 机制(Service Provider Interface),从类路径下的META-INF/spring.factories文件中加载配置的初始化器(ApplicationContextInitializer)和监听器(ApplicationListener)。例如,spring.factories中可能包含:properties
org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\ org.springframework.boot.context.ContextIdApplicationContextInitializer
三、SpringApplication.run(args)核心流程
run()方法是启动的核心,可分为准备阶段、上下文创建与刷新、启动完成三个阶段,源码简化如下:
java运行
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start(); // 计时开始
// 1. 初始化运行监听器和应用上下文
ConfigurableApplicationContext context = null;
Collection<SpringApplicationRunListener> listeners = getRunListeners(args);
listeners.starting(); // 发布启动事件(ApplicationStartingEvent)
try {
// 2. 准备环境(配置、参数等)
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
// 3. 打印Banner(控制台图标)
Banner printedBanner = printBanner(environment);
// 4. 创建应用上下文(根据应用类型)
context = createApplicationContext();
// 5. 准备上下文(关联环境、注册主类等)
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 6. 刷新上下文(核心!Spring容器初始化+SpringBoot扩展)
refreshContext(context);
// 7. 刷新后的操作(空实现,供扩展)
afterRefresh(context, applicationArguments);
stopWatch.stop(); // 计时结束
listeners.started(context); // 发布启动完成事件(ApplicationStartedEvent)
// 8. 执行Runner(CommandLineRunner/ApplicationRunner)
callRunners(context, applicationArguments);
listeners.running(context); // 发布运行中事件(ApplicationReadyEvent)
} catch (Throwable ex) {
handleRunFailure(context, ex, listeners); // 处理启动失败
throw new IllegalStateException(ex);
}
return context;
}
关键步骤详解:
1. 准备环境(prepareEnvironment)
- 作用:初始化应用环境(包含配置文件、系统变量、命令行参数等)。
- 流程:
- 创建
ConfigurableEnvironment(根据应用类型,如StandardServletEnvironment)。 - 加载配置(
application.properties/yml、系统变量、args参数等)。 - 发布
ApplicationEnvironmentPreparedEvent事件,监听器(如配置文件加载器)会处理该事件。
- 创建
2. 创建应用上下文(createApplicationContext)
- 作用:根据应用类型创建对应的
ApplicationContext(Spring 容器的核心)。 - 逻辑:
java运行
protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.webApplicationType.getApplicationContextClass(); return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }SERVLET应用:创建AnnotationConfigServletWebServerApplicationContextREACTIVE应用:创建AnnotationConfigReactiveWebServerApplicationContext- 非 Web 应用:创建
AnnotationConfigApplicationContext
3. 准备上下文(prepareContext)
- 作用:将环境、监听器、主类等关联到上下文,并加载主类作为配置类。
- 关键逻辑:
- 关联环境:
context.setEnvironment(environment) - 执行初始化器:
applyInitializers(context)(初始化器对上下文进行预处理) - 发布
ApplicationContextInitializedEvent事件 - 注册主类:将
@SpringBootApplication标注的主类注册到容器,作为配置类
- 关联环境:
4. 刷新上下文(refreshContext)
这是最核心的步骤,本质是调用 Spring 的AbstractApplicationContext.refresh()方法(Spring 容器初始化的标准流程),同时 SpringBoot 在此基础上扩展了内嵌容器启动的逻辑。
java运行
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context); // 调用Spring的refresh()
if (this.registerShutdownHook) {
try {
context.registerShutdownHook(); // 注册关闭钩子
} catch (AccessControlException ex) {
// 忽略权限异常
}
}
}
-
Spring 原生
refresh()流程(核心步骤):prepareRefresh():准备刷新(验证环境、初始化属性源等)obtainFreshBeanFactory():创建 BeanFactoryinvokeBeanFactoryPostProcessors():执行 BeanFactory 后置处理器(如解析@Configuration类、处理@ComponentScan扫描 Bean)registerBeanPostProcessors():注册 Bean 后置处理器(用于 Bean 的初始化前后增强)initMessageSource():初始化消息源(国际化)initApplicationEventMulticaster():初始化事件多播器onRefresh():SpringBoot 扩展点(内嵌容器启动在此执行)registerListeners():注册监听器finishBeanFactoryInitialization():实例化所有非懒加载的单例 BeanfinishRefresh():完成刷新(发布ContextRefreshedEvent)
-
SpringBoot 的
onRefresh()扩展:对于 Web 应用(如ServletWebServerApplicationContext),onRefresh()会调用createWebServer()创建内嵌 Web 服务器(Tomcat/Undertow/Jetty):java运行
@Override protected void onRefresh() { super.onRefresh(); try { createWebServer(); // 创建Web服务器 } catch (Throwable ex) { throw new ApplicationContextException("Unable to start web server", ex); } } private void createWebServer() { WebServer webServer = this.webServer; ServletContext servletContext = getServletContext(); if (webServer == null && servletContext == null) { // 1. 获取Web服务器工厂(如TomcatServletWebServerFactory) ServletWebServerFactory factory = getWebServerFactory(); // 2. 创建Web服务器(如Tomcat) this.webServer = factory.getWebServer(getSelfInitializer()); } else if (servletContext != null) { try { getSelfInitializer().onStartup(servletContext); } catch (ServletException ex) { throw new ApplicationContextException("Cannot initialize servlet context", ex); } } initPropertySources(); }
5. 执行 Runner(callRunners)
启动完成后,会执行所有CommandLineRunner和ApplicationRunner接口的实现类,用于在应用启动后执行自定义逻辑(如数据初始化):
java运行
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); // 按@Order排序
for (Object runner : new LinkedHashSet<>(runners)) {
if (runner instanceof ApplicationRunner) {
callRunner((ApplicationRunner) runner, args);
} else if (runner instanceof CommandLineRunner) {
callRunner((CommandLineRunner) runner, args);
}
}
}
四、核心原理总结
- SPI 机制:通过
META-INF/spring.factories加载初始化器、监听器、自动配置类,实现 "插件化" 扩展。 - 事件驱动:通过
SpringApplicationRunListener在启动各阶段发布事件(如ApplicationStartingEvent、ApplicationReadyEvent),允许监听器介入启动过程。 - 自动配置:在
refresh()阶段,@EnableAutoConfiguration通过AutoConfigurationImportSelector加载spring.factories中的自动配置类(如DataSourceAutoConfiguration),结合@Conditional条件注解实现按需配置。 - 内嵌容器:通过
onRefresh()扩展点创建内嵌 Web 服务器(Tomcat 等),无需外部容器部署。
通过以上机制,SpringBoot 实现了 "零配置" 启动,大幅简化了 Spring 应用的开发与部署。
1556

被折叠的 条评论
为什么被折叠?



