【问题标题】:Can Spring PropertyPlaceholderConfigurer ignore unset properties in a property location path?Spring PropertyPlaceholderConfigurer 可以忽略属性位置路径中未设置的属性吗?
【发布时间】:2015-03-07 19:14:46
【问题描述】:

我有一个在 tomcat 容器中运行的基于 Spring 的 Web 应用程序,我想维护两个不同的配置文件:

  • src/main/resources/default.properties:包含开发、集成测试以及未设置其他属性时的默认值
  • .../tomcat/conf/app.properties:在不同的环境下有不同的内容,应该覆盖default.properties

当应用程序在 tomcat 中运行时,我有一个运行良好的 spring 配置

app-context.xml

<context:property-placeholder
    ignore-resource-not-found="true"
    ignore-unresolvable="true"
    system-properties-mode="OVERRIDE"
    location="classpath:default.properties,
              file:${catalina.home}/conf/app.properties"/>

但是当我尝试在集成测试中使用此配置时,在 tomcat 容器之外,加载 applicationContext 失败:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    ...
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0' defined in null: Could not resolve placeholder 'catalina.home'
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:287)

当当前上下文中没有设置属性 catalina.home 时,有什么方法可以告诉 Spring 忽略位置文件:${catalina.home}/conf/app.properties?

【问题讨论】:

  • 试试PP default value:file:${catalina.home:}/conf/app.properties
  • @ArtemBilan 确实成功了,但我无法将您的评论标记为答案。您可以将其发布为答案吗?
  • 完成。感谢您的确认!我不确定,beucase 从来没有满足过类似的要求 :-)。

标签: java spring tomcat configuration environments


【解决方案1】:

你可以设置ignore-resource-not-found来解决这个问题。

试试这个:

<context:property-placeholder
    ignore-resource-not-found="true"
    ignore-unresolvable="true"
    system-properties-mode="OVERRIDE"
    location="classpath:default.properties,
              file:${catalina.home}/conf/app.properties"/>

来自 spring-context.xsd 的代码 sn-p:

<xsd:attribute name="ignore-resource-not-found" type="xsd:boolean" default="false">
            <xsd:annotation>
                <xsd:documentation><![CDATA[
    Specifies if failure to find the property resource location should be ignored.
    Default is "false", meaning that if there is no file in the location specified
    an exception will be raised at runtime.
                ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>

【讨论】:

  • ignore-resource-not-found 和 ignore-unresolvable 已设置,但是当缺少的属性是位置的一部分时,这似乎没有帮助。事实证明,一个空的默认值确实有帮助
【解决方案2】:

由于您有一个上下文,其中一些 PP 值可能无法解析,您可以尝试为属性使用default variant:

file:${catalina.home:}/conf/app.properties

在这种情况下,如果没有 catalina.home 属性,则路径前缀将解析为空字符串,例如在System.getProperties(),我猜是Tomcat的情况。

然后ignore-resource-not-found="true" 会做这些事情。

【讨论】:

    【解决方案3】:

    我的建议是使用 spring 配置文件来启用/禁用属性文件。

    当在“tomcat”或“web”配置文件上运行 tomcat swith 并将 app.properties 的加载放入该配置文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2018-10-31
      • 2012-06-30
      • 2019-12-14
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多