【问题标题】:Organizing Spring Boot profiles into directories将 Spring Boot 配置文件组织到目录中
【发布时间】:2020-10-27 15:41:27
【问题描述】:

我有一个 Spring Boot 项目,其中包含我们使用的一些不同配置,这些配置基于配置文件。到目前为止,这对我来说效果很好,但问题是我在resources 目录中获得了相当多的配置文件。我想将它们组织到目录中,以便更容易快速找到它们。我试过这样做,但是当将它们移动到嵌套目录时,Spring 无法实际加载给定配置文件的值。

前结构:

resources
  database
    application-liqudbase.yaml
    application-embedded.yaml
  environments
    application-test.yaml
    application-prod.yaml

在这个例子中,我会将test.yaml 设置为spring.profiles.include: database/embedded.yaml。这个问题是即使没有引发错误,它也不会加载embedded.yaml 文件中的值。使用默认的 Spring 配置文件设置是否有可能?

【问题讨论】:

    标签: spring spring-boot kotlin


    【解决方案1】:

    我怀疑您要查找的是 spring.config.location 属性。这是一个以逗号分隔的目录列表,用于定位您的配置文件。在上面的示例中,它将是 spring.config.location=classpath:/database/,classpath:/environments/

    请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-application-property-files 了解更多信息。

    【讨论】:

    • 这很棒!我不敢相信我没有在文档中看到它,因为我在发布问题之前正在阅读该页面......非常感谢。对于尝试在 Spring Boot 中使用 Gradle 执行此操作的任何人,我最终将其添加到 bootRun 任务的 jvmArgs,因此它对于项目来说是不变的。
    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多