温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot bootstrap.yml配置未生效怎么解决

发布时间:2020-10-29 22:25:08 来源:亿速云 阅读:2295 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关SpringBoot bootstrap.yml配置未生效怎么解决,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<!--需要引入该jar才能使bootstrap配置文件生效-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-context</artifactId>
  </dependency>

补充知识:SpringBoot不读取bootstrap.yml/properties文件

今天写创建了一个SpringBoot项目,配置文件从其他项目拷贝了一份bootstrap.yml

之前一直用的application.yml

心想:application.yml 优先级没有bootstrap.yml 高,bootstrap配置文件肯定没问题

项目一跑来,发现配置文件里面的内容没读取到。

之后通过各种查资料,才明白了application.yml 和bootstrap.yml 的区别,不仅仅是优先级的问题。

先说我的问题是什么原因吧:

SpringBoot 项目中如果没有依赖 spring-cloud-context 的话,是不会读取bootstrap.properties 文件

也就是说

bootstrap.yml配置是SpringCloud项目才会用到的。

如果你的项目仅仅是一个SpringBoot项目,只会识别application.yml配置文件。

由于SpringCloud是基于SpringBoot构建的,所有SpringCloud项目两种文件都会识别,这个时候才有优先级的说法,SpringCloud项目是会优先读取bootstrap配置在读取application配置。

引用Spring官网的一句话

A Spring Cloud application operates by creating a “bootstrap” context, which is a parent context for the main application. Out of the box it is responsible for loading configuration properties from the external sources, and also decrypting properties in the local external configuration files.

SpringCloud 应用是基于 一个 “bootstrap”的上下文运行的。

也就说明了 bootstrap这个配置是SpringCloud 应用才会用的。

而且这个配置是不能被覆盖的,也就是优先级比application配置中的高。

什么配置在application配置?什么配置在bootstrap里面配置?

1、当使用 Spring Cloud Config Server 的时候,spring.application.name 和 spring.cloud.config.server.git.uri应该在 bootstrap.yml 里面指定

2、一些加密解密的配置也应该配置在bootstrap里面

以上就是SpringBoot bootstrap.yml配置未生效怎么解决,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI