特定于配置文件的spring.config.additional-location?

时间:2019-12-05 23:00:24

标签: spring spring-boot configuration-files spring-profiles

我通过在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,但是如果无法控制属性文件名或位置,我认为这对我没有帮助。

1 个答案:

答案 0 :(得分:0)

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

文档状态:

  

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

也:

  

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

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

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