【问题标题】:Conventions for naming application.properties命名 application.properties 的约定
【发布时间】:2018-01-26 20:21:31
【问题描述】:

在我的 Spring 应用程序中,我正在尝试将属性占位符与配置文件 testdevprod 一起使用。此外,我希望能够加载所有配置文件通用的默认属性 common

<context:property-placeholder
            ignore-resource-not-found="false"
            location="classpath:application-common.properties,classpath:application-test.properties"/>

但是,这不能正常工作。我还没有使用变量${spring.profiles.active},因为即使没有它也无法正常工作。发生的情况是连字符 application- 之后的任何内容都按字母顺序加载。加载的只是第一个,另一个被忽略。所以在这种情况下,只加载了-common。奇怪的是,如果我删除连字符,它会同时加载两个文件。

是否有一些我不知道的隐藏行为?

【问题讨论】:

  • 您一次需要多少个属性文件,为什么?想想吧。
  • 在每次运行中我只需要两个 - 所有配置文件的公共属性,然后是当前选定配置文件的属性。为什么投反对票这么糟糕?
  • application.properties 是常见的...您正在围绕框架而不是框架工作。 Spring boot 将加载application.properties 和您的application-{profile}.properties(并按此顺序)。通过尝试比框架更聪明,您正在使事情变得更加复杂。
  • 我没有使用 Spring Boot,我只使用 Spring Context。我应该如何描述属性占位符以在您编写时加载 application.properties 和 application-{profile}.properties?我尝试了classpath:application.properties,classpath:application-${spring.profiles.active}.properties,但在这种情况下,只加载了application.properties,而忽略了-test
  • 请添加一些关于使用的 Spring 版本的信息。

标签: spring


【解决方案1】:
  1. 您可以使用@PropertySource 加载“common”属性文件。

    @PropertySource({ "classpath:application-common.properties"
    })

  2. 在运行应用程序时使用spring.profiles.active 加载环境特定属性文件。

    例如,spring.profiles.active=dev

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2010-10-31
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2011-06-17
    • 2011-02-22
    • 2011-02-07
    相关资源
    最近更新 更多