gpt4 book ai didi

Spring "spring.profiles.include"覆盖

转载 作者:IT老高 更新时间:2023-10-28 13:46:33 31 4
gpt4 key购买 nike

我的意图是在 Spring Boot 应用程序中有两个配置文件 - 开发和生产一个。开发配置文件只是为了覆盖生产配置文件的一些变量(例如内存数据库而不是云中的数据库)。由于我预计将来会对生产配置文件进行一些更改,因此在开发配置文件中复制变量似乎不是一个解决方案。

所以,在 Spring Reference我读到 spring.profiles.include 应该只从引用的配置文件中添加属性。

Sometimes, it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles.

但是,根据我的检查,它会覆盖它。因此,当有两个配置文件 foo 和 bar 时,在单独的 yaml 文件中:

应用程序-foo.yaml:

myproperty: 44

application-bar.yaml:

spring:
profiles:
include: foo
active: bar,foo
myproperty: 55

并在IDE中设置-Dspring.profiles.active=bar变量,myproperty的运行时值为44。也就是说bar , 被 foo 覆盖,它应该只添加属性,而不是覆盖它们。启动应用程序时,我得到:

The following profiles are active: foo,bar

我按照 answer 的建议将 spring.profiles.active=bar 添加到 application-bar.yaml ,在另一个问题中,但它没有效果 - 属性是否存在没有区别(我也尝试使用破折号列表而不是逗号分隔值)。

我的问题是,它应该如何工作(那么 Spring Reference 具有误导性)?如果是这样,有什么解决办法吗?

添加 link到github上的应用程序源代码。

最佳答案

我们以稍微不同的方式实现 Spring 事件配置文件。假设默认属性文件 application.yml 包含在生产环境和开发环境中相同的所有默认值。

分别为名为 application-prd.ymlapplication-dev.yml 的生产和开发文件创建单独的属性。这些文件可能包含额外的属性或覆盖一些默认属性。

在应用程序启动期间,我们将 spring.profiles.active 作为环境变量传递。例如,

-Dspring.profiles.active=prd

将获取 application-prd.yml 以及 application.yml

-Dspring.profiles.active=dev

将获取 application-dev.yml 以及 application.yml

关于Spring "spring.profiles.include"覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47368086/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com