特定于Profile的spring.config.additional-location?

问题描述 投票:1回答:1

我通过在spring.config.additional-location中设置SpringApplicationBuilder,为Spring Boot应用程序指定了一个外部属性文件。

new SpringApplicationBuilder(MyApplication)
            .properties(['spring.config.additional-location': myExternalProperties])
            .run(myArgs)

此方法在允许我使用application.properties覆盖myExternalProperties文件中的属性的范围内起作用。

但是,myExternalProperties会依次被任何特定于配置文件的属性所覆盖,例如application-myProfile.properties

我理解这与Spring对Externalized Configuration的优先级一致,但我希望myExternalProperties甚至覆盖特定于配置文件的属性。

如何实现该优先级?我无法控制myExternalProperties的文件名或位置。此变量是在环境中预设的系统属性。

我一直在看Profile-specific Properties,尤其是这个报价。

如果在spring.config.location中指定了任何文件,则不考虑这些文件的特定于配置文件的变体。如果您还想使用特定于配置文件的属性,请使用spring.config.location中的目录。

[我认为此注释同样适用于spring.config.additional-location,但是我无法控制属性文件名或位置,对我没有帮助。

spring spring-boot configuration-files spring-profiles
1个回答
0
投票

恐怕无法通过非骇客的方式实现。

文档状态:

特定于配置文件的文件始终会覆盖非特定文件。

也:

如果指定了多个配置文件,则采用后赢策略。例如,在通过SpringApplication API配置的配置文件之后,添加了spring.profiles.active属性指定的配置文件,因此具有优先权。

您是否可以考虑将基于配置文件的配置用于myExternalProperties?

您甚至可以使用env vars作为占位符。希望这会有所帮助:Env vars in Spring boot properties

© www.soinside.com 2019 - 2024. All rights reserved.