基于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里面
英文好的可以参考官网文章:
https://cloud.spring.io/spring-cloud-commons/multi/multi__spring_cloud_context_application_context_services.html#customizing-bootstrap-properties
以上这篇基于SpringBoot bootstrap.yml配置未生效的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
代码知识SEO上一篇 : spring boot 若依系统整合Ueditor部署时上传图片错误问题
下一篇 : idea springboot远程debug的操作方法
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!