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

标签 spring-boot

Spring 启动版本:2.5.0

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

  1. 应用程序属性
  2. application-dev.properties
  3. application-prod.properties

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

spring.profiles.active=@prodProfile@
spring.config.import=optional:file:./application-local.properties

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

    <properties>
        <prodProfile>prod</prodProfile>
    </properties>

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

最佳答案

是的,这是预期的行为。作为described in the documentation ,使用 spring.config.import 导入的文件“被视为附加文档,插入到声明导入的文档下方”。

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

关于spring-boot - Spring 引导配置文件特定和 spring.config.import 属性的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68760290/

相关文章:

java - 为多模块微服务 Spring Boot 应用程序设置 swagger 文档

java - Spring Controller 不响应 https

java - 如何在 Spring Boot 和 Hibernate 中统计特定 id 的记录

java - 我可以在 Spring Boot 中为函数添加 "get"前缀吗

java - 当类路径包含许多目录时,类路径根是什么意思?

spring - 在thymeleaf中获取spring应用环境

java - Spring Boot 2 中缺少 TomcatEmbeddedServletContainerFactory

java - 为什么我的 ApplicationContext 在我启用网络安全时无法加载?

angular - 使用 Spring Boot 和 Angular 2 CLI 进行 Google OAuth2 身份验证

java - 如何避免Zuul成为瓶颈