欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于SpringBoot bootstrap.yml配置未生效的解决

时间:2021-06-04 07:54:42|栏目:JAVA代码|点击:

我就废话不多说了,大家还是直接看代码吧~

<!--需要引入该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

上一篇:java多线程编程之慎重使用volatile关键字

栏    目:JAVA代码

下一篇:浅谈spring容器中bean的初始化

本文标题:基于SpringBoot bootstrap.yml配置未生效的解决

本文地址:http://www.codeinn.net/misctech/135176.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有