Springboot 中application.yml和bootStrap.yml 的加载顺序

1. 若application.yml 和bootStrap.yml 在同一目录下,则bootStrap.yml 的加载顺序要高于application.yml,即bootStrap.yml  会优先被加载。

   原理:bootstrap.yml 用于应用程序上下文的引导阶段。

              bootstrap.yml 由父Spring ApplicationContext加载。

            •bootstrap.yml 可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。
            •application.yml 可以用来定义应用级别的,如果搭配 spring-cloud-config 使用 application.yml 里面定义的文件可以实现动态替换。

            使用Spring Cloud Config Server时,应在 bootstrap.yml 中指定:

?
1
2
spring.application.name
spring.cloud.config.server.git.uri

2. 不同位置的配置文件的加载顺序:

   在不指定要被加载文件时,默认的加载顺序:由里向外加载,所以最外层最后被加载,会覆盖里层的属性(参考官网介绍)

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment

  1. /config subdirectory of the current directory.    //位于与jar包同级目录下的config文件夹,
  2. The current directory                             //位于与jar包同级目录下
  3. A classpath /config package          //idea 环境下,resource文件夹下的config文件夹
  4. The classpath root                                //idea 环境下,resource文件夹下  (1->4, 外->里

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

3. 可以通过属性指定加载某一文件:

 java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

当通过spring.config.location 指定一个配置文件时,配置文件的搜索顺序如下:

  1. file:./custom-config/
  2. classpath:custom-config/
  3. file:./config/
  4. file:./
  5. classpath:/config/
  6. classpath:/

最下层的优先加载,所以最上层的属性会覆盖下层的属性;

4. 如果使用spring-cloud-config时,项目内部的resource下有bootstrap.yml文件,并且在bootstrap.yml 里配置spring.application.name, git.url,spring.active.profies. 将项目打成jar包,放到服务器上,与jar包并列的位置,有start.sh脚本, 

a. 在start 脚本里指定了配置文件:spring.config.location=./bootstrap.yml, 则配置文件的加载顺序将为:

1. cloud-config 仓库里指定的yml 配置;

2. ./bootstrap.yml

3. classpath:/bootstrap.yml

4. 外部application.yml

5. 内部application.yml

b. 在start 脚本里指定了配置文件:spring.config.location=./application.yml, 则配置文件的加载顺序将为:

1. cloud-config 仓库里指定的yml 配置;

2. ./application.yml

3.  classpath:/application.yml

4. ./bootstrap.yml

5. classpath:/bootstrap.yml

所以,不管是jar包内还是jar运行的同级目录下,只要包含bootstrap.yml ,且为云配置,则云配置文件会覆盖其他配置文件;





  • 5
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值