SpringBoot2.7.9 配置文件加载方式

ConfigDataLocationResolver接口方法说明
isResolvable: 判断是否是需要转换的资源
resolve: 将单个ConfigDataLocation转换为ConfigDataResource集合,在激活环境配置之前加载,也就是profile文件加载之前加载
resolveProfileSpecific: 将单个ConfigDataLocation转换为ConfigDataResource集合,在加载环境配置时(Profiles)进行加载
// 调用resolve将单个ConfigDataLocation转换为ConfigDataResource集合,并且加载文件内容
org.springframework.boot.context.config.ConfigDataImporter#resolveAndLoad

ConfigDataLoader接口方法说明
isLoadable: 是否获取符合条件的ConfigDataLoader
load: 加载配置

ConfigDataResource抽象类:用来描述配置数据资源 optional: 表示是否有指定前缀标记

spring.main.cloud-platform: 配置cloud平台名称
spring.config.name: 配置要加载配置文件的名称,默认值是["application"] org.springframework.boot.context.config.StandardConfigDataLocationResolver#CONFIG_NAME_PROPERTY
spring.config.import、spring.config.additional-location、spring.config.location: 指定要加载的配置文件,优先级如下:
spring.config.import 
spring.config.additional-location
spring.config.location
可以配置多个,默认使用英文分号 ; 分割,也可以自定义解析器(ConfigDataLocationResolver)进行拆分,加载顺序按照拆分后的顺序进行加载,比如:
spring.config.import=import.yaml;import2.yaml;import3.yaml

当配置了spring.config.location时,spring.config.name不生效
当配置了spring.config.location时,默认的org.springframework.boot.context.config.ConfigDataEnvironment#DEFAULT_SEARCH_LOCATIONS不会再加载
org.springframework.boot.context.config.ConfigDataEnvironment#DEFAULT_SEARCH_LOCATIONS包含:
optional:classpath:/;optional:classpath:/config/ 和 optional:file:./;optional:file:./config/;optional:file:./config/*/

spring.config.on-not-found: 配置文件不存在时的策略,参考 org.springframework.boot.context.config.ConfigDataNotFoundAction 枚举类,可以通过SpringApplication.setDefaultProperties 进行设置

spring.profiles.include: 配置要加载的环境, 多个使用逗号分割  比如  a,b 是a和b两个环境
spring.profiles.group: 配置的环境组  spring.profiles.group.x=d,f spring.profiles.group.y=e,g
spring.profiles.active: 激活的环境  多个使用逗号分割  比如  a,b 是a和b两个环境
spring.profiles.default: 默认加载的环境 多个使用逗号分割  比如  a,b 是a和b两个环境
spring.profiles.include和spring.profiles.active都会放在spring.profiles.active集合中
spring.profiles.active和spring.profiles.default同时配置时,默认使用spring.profiles.active,未配置spring.profiles.active时使用spring.profiles.default

profile: 控制拼接指定环境的配置文件,如 dev 生成的配置文件名称为  application-dev.yaml

在springboot中配置文件的加载顺序如下:
yaml yml xml properties
spring.config.name=application(默认配置)
内部默认的配置文件加载顺序
file:/application.yaml
file:/application.yml
file:/application.xml
file:/application.properties
file:./config/application.yaml
file:./config/application.yml
file:./config/application.xml
file:./config/application.properties
file:./config/*/application.yaml
file:./config/*/application.yml
file:./config/*/application.xml
file:./config/*/application.properties
classpath:/application.yaml
classpath:/application.yml
classpath:/application.xml
classpath:/application.properties
classpath:./config/application.yaml
classpath:./config/application.yml
classpath:./config/application.xml
classpath:./config/application.properties
// 默认是 profile是default
file:/application-default.yaml
file:/application-default.yml
file:/application-default.xml
file:/application-default.properties
file:./config/application-default.yaml
file:./config/application-default.yml
file:./config/application-default.xml
file:./config/application-default.properties
file:./config/*/application-default.yaml
file:./config/*/application-default.yml
file:./config/*/application-default.xml
file:./config/*/application-default.properties

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值