Spring Cloud之Config配置中心之本地配置文件加载原理分析

SpringCloud总体介绍

Config使用
Config本地配置文件加载原理分析
Config配置中心配置加载原理分析

本文主要内容:

  1. 简要了解PropertySource配置源体系结构
  2. 了解Springboot中,每个PropertySource优先级
  3. 了解Springboot是如何加载application.properties配置文件,即:ConfigFileApplicationListener
  4. 了解SpringCloud中的BootstrapApplicationListener及作用

一、Spring本地配置文件加载

1.1、Spring PropertySource配置源体系结构

在这里插入图片描述
其中有一些我们常见的属性源,如:

  • CommandLinePropertySource命令行属性源
  • SystemEnvironmentPropertySource系统环境变量属性源
  • CompositePropertySource组合属性源
  • MutablePropertySources可变属性源

二、SpringBoot本地配置文件加载

2.1、官方定义SpringBoot配置源加载顺序

因为PropertySource是存在一个List中的,因此PropertySource之间是有序的,而在Springboot官网中的(2.Externalized Configuration)章节,对PropertySource的优先级顺序进行了说明:

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

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

  2. @TestPropertySource annotations on your tests.

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

  4. Command line arguments.

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

  6. ServletConfig init parameters.

  7. ServletContext init parameters.

  8. JNDI attributes from java:comp/env.

  9. Java System properties (System.getProperties()).

  10. OS environment variables.

  11. A RandomValuePropertySource that has properties only in random.*.

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

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

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

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

  16. @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.

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

其中我们常用的有:

<
加载顺序 资源类型 设置方式
4 Command line arguments 命令行参数,通过java -jar --name=”Spring”
6 ServletConfig init parameters ServletConfig配置的init参数
7 ServletContext init parameters ServletContext配置的init参数
9
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值