栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringApplication 启动流程及各类事件生命周期那点事

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringApplication 启动流程及各类事件生命周期那点事

前言

本文通过Springboot启动方法分析SpringApplication逻辑。从静态run方法执行到各个阶段发布不同事件完成整个应用启动。

SpringApplication 源码注释中说明了该类的主要作用,如下所示

 其他Spingboot 原理相关文章请参考

手把手分析Springboot @EnableScheduling原理

SpringCloud FeignClient底层实现原理(一)

springboot底层如何加载配置文件-看完你就知道了

启动逻辑详解

springboot应用在启动时存在两种方式:

第一种通过静态run方法直接启动

SpringApplication.run(MyApplication.class, args);

第二种通过创建对象后执行run方法启动

SpringApplication application = new SpringApplication(MyApplication.class);
application.run(args)

第二种方法其实就是第一种方法背后实现逻辑。

在run方法执行过程中大量使用事件机制完成日志,配置文件,激活profile等工作,下图为整体执行流程图。

 在初始化SpringApplication对象时通过springboot SPI方式对classpath下所有jar包中

META-INF/spring.factories文件进行加载,这也是springboot自动装配中使用到的逻辑。

 在构造方法中 通过getSpringFactoriesInstances完成工厂类加载与初始化。

private  Collection getSpringFactoriesInstances(Class type) {
    return getSpringFactoriesInstances(type, new Class[] {});
}
private  Collection getSpringFactoriesInstances(Class type, Class[] parameterTypes, Object... args) {
	ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
	Set names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
	List instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
	AnnotationAwareOrderComparator.sort(instances);
	return instances;
}
getSpringFactoriesInstances方法中通过一个单例类SpringFactoriesLoader完成所有META-INF/spring.factories文件的读取。通过classLoader.getResources方法读取classpath下所有jar包后队文件进行解析。

spring.factories文件内容为key value对,多个value用逗号分隔,如下所示

# Spring Test ContextCustomizerFactories
org.springframework.test.context.ContextCustomizerFactory=
org.springframework.boot.test.context.ImportsContextCustomizerFactory,
org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizerFactory,
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory,
org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory,
org.springframework.boot.test.web.client.TestRestTemplateContextCustomizerFactory,
org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizerFactory

# Test Execution Listeners
org.springframework.test.context.TestExecutionListener=
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener,
org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener

# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=
org.springframework.boot.test.web.SpringBootTestRandomPortEnvironmentPostProcessor

 接下来是启动逻辑的重点run方法。本文重点分析run方法中事件触发逻辑。

事件机制详解

 在run方法中通过getRunListeners方法加载事件监听器,该监听器负责发布应用启动中各个阶段的状态

  1. starting 阶段
  2. environmentPrepare阶段 负责配置文件,系统环境变量,激活profile等逻辑
  3. contextPrepared 阶段负责初始化applicationContext
  4. contextLoaded 阶段 applicationContext加载所有单例bean
  5. started 阶段 应用启动完成
  6. running 阶段 应用运行中状态

getRunListeners通过getSpringFactoriesInstances方法加载 SpringApplicationRunListener所有实现类,该接口默认实现类为 EventPublishingRunListener,该类包含如下方法

 这下方法中通过 SimpleApplicationEventMulticaster类发送通知,该类名也能表示其作用:简单应用事件多播器,翻译出来总感觉差点意思。

 

 在run 方法中存在以上6个listener方法调用的逻辑。具体发动哪些事件请看下图

 上图中列出了监听器处理的部分事件,其他事件感兴趣的话可以通过源码进行分析。

总结

通过整体启动分析将整个启动流程进行梳理,有助于大家在阅读源码前掌握正义的概要避免陷入局部逻辑无法自拔。

后面将会针对不同逻辑进行深入分析。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号