当前位置: 首页 > news >正文

SpringBoot源码启动流程(待完善)

SpringBoot源码启动流程

1. 构造SpringApplication对象

1.1 推测web应用类型

判断关键类是否存在来区分类型

  • REACTIVE
  • NONE
  • SERVLET
	static WebApplicationType deduceFromClasspath() {if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {return WebApplicationType.REACTIVE;}for (String className : SERVLET_INDICATOR_CLASSES) {if (!ClassUtils.isPresent(className, null)) {return WebApplicationType.NONE;}}return WebApplicationType.SERVLET;}

2. 设置Initializers和Listeners扩展点

3. 推测Main方法所在类

	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {this.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));// 推测web应用this.webApplicationType = WebApplicationType.deduceFromClasspath();// 设置Initializer和Listener扩展点setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));// 推测main方法所在类this.mainApplicationClass = deduceMainApplicationClass();}

2. run(String... args)方法

SpringApplicationRunListener.starting

SpringBoot提供了一个EventPublishingRunListener,它实现了SpringApplicationRunListener接口,默认情况下会利用EventPublishingRunListener发布一个ApplicationContextInitializedEvent事件,程序员可通过定义ApplicationListener来消费这个事件

		SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting();

创建Environment

解析配置文件, 环境变量, 启动命令参数

 

SpringApplicationRunListeners.environmentPrepared

用EventPublishingRunListener发布一个ApplicationEnvironmentPreparedEvent事件,默认情况会有一个EnvironmentPostProcessorApplicationListener来消费这个事件,而这个ApplicationListener接收到这个事件之后,就会解析application.properties、application.yml文件,并添加到Environment对象中去。

打印Banner

创建Spring容器

	protected ConfigurableApplicationContext createApplicationContext() {Class<?> contextClass = this.applicationContextClass;if (contextClass == null) {try {switch (this.webApplicationType) {case SERVLET:contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);break;case REACTIVE:contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);break;default:contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);}}catch (ClassNotFoundException ex) {throw new IllegalStateException("Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass",ex);}}return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);}

配置解析流程

SpringBoot配置优先级

官网描述:Core Features

 

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).

  2. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.

  3. Config data (such as application.properties files).

  4. RandomValuePropertySource that has properties only in random.*.

  5. OS environment variables.

  6. Java System properties (System.getProperties()).

  7. JNDI attributes from java:comp/env.

  8. ServletContext init parameters.

  9. ServletConfig init parameters.

  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).

  11. Command line arguments.

  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

  13. @DynamicPropertySource annotations in your tests.

  14. @TestPropertySource annotations on your tests.

  15. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

Config data files are considered in the following order:

  1. Application properties packaged inside your jar (application.properties and YAML variants).

  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).

  3. Application properties outside of your packaged jar (application.properties and YAML variants).

  4. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).

http://www.lryc.cn/news/281732.html

相关文章:

  • 存内计算技术打破常规算力局限性
  • uniapp微信小程序投票系统实战 (SpringBoot2+vue3.2+element plus ) -投票帖子明细实现
  • RabbitMQ入门到实战——高级篇
  • 05.阿里Java开发手册——前后端规约
  • Linux网络服务部署yum仓库
  • 智慧工地AI识别安全预警解决方案---豌豆云
  • 红队打靶练习:TOMMY BOY: 1
  • Springboot中的@DependsOn注解
  • Django教程第5章 | Web开发实战-数据统计图表(echarts、highchart)
  • 【机器学习 西瓜书】期末复习笔记整理
  • 回归预测 | Matlab基于SO-GRU蛇群算法优化门控循环单元的数据多输入单输出回归预测
  • 自然语言处理实战项目25-T5模型和BERT模型的应用场景以及对比研究、问题解答
  • 分布式搜索——Elasticsearch
  • 用python实现调用nosql
  • setTimeout和setInterval定时器的返回值
  • C/C++指针
  • 2024 基于 Rust 的 linter 工具速度很快
  • JWT相关问题及答案(2024)
  • Linux例行性工作 at和crontab命令
  • cookie共享和session共享实例演示
  • 设计模式之开闭原则:如何优雅地扩展软件系统
  • Python Pandera 用于数据验证和清洗:是一个强大的工具用起来
  • 英诺赛科推出BMS方案,搭载100V双向导通VGaN
  • 如何用Mac工具制作“苹果高管形象照”
  • 回环检测算法:Stable Trangle Descriptor
  • MetaGPT入门(二)
  • AI嵌入式K210项目(4)-FPIOA
  • FPGA开发设计
  • 上海亚商投顾:沪指冲高回落 旅游板块全天强势
  • Linux网络--- SSH服务