Spring boot 配置文件特定属性和 spring.config.import 属性的优先级

cod*_*247 6 spring-boot

春季启动版本:2.5.0

我有不同的属性文件。jar 内有 3 个,服务器上有 1 个(用于覆盖特定于服务器的属性)

  1. 应用程序属性
  2. 应用程序-dev.properties
  3. 应用程序产品属性

活动配置文件和外部属性的路径在 application.properties 中配置。

spring.profiles.active=@prodProfile@
spring.config.import=optional:file:./application-local.properties
Run Code Online (Sandbox Code Playgroud)

其中 @prodProfile@ 的值来自 pom.xml 的配置文件配置部分。

    <properties>
        <prodProfile>prod</prodProfile>
    </properties>
Run Code Online (Sandbox Code Playgroud)

如果我在 application-prod.properties 和 application-local.properties 中有相同的属性,哪个优先?当我测试时,我发现配置文件特定具有优先权。无法使用 spring.config.import 覆盖属性值。这是预期的行为吗?

And*_*son 4

是的,这是预期的行为。如文档中所述,使用导入的文件spring.config.import“被视为紧邻声明导入的文件下方插入的附加文档”。

在您的情况下,您将文件导入,application.properties因此它将被视为常规的、非特定于配置文件的配置。因此,特定于配置文件的属性将优先。