Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring PropertyPlaceHolderConfigure能否忽略属性位置路径中未设置的属性?_Java_Spring_Tomcat_Configuration_Environments - Fatal编程技术网

Java Spring PropertyPlaceHolderConfigure能否忽略属性位置路径中未设置的属性?

Java Spring PropertyPlaceHolderConfigure能否忽略属性位置路径中未设置的属性?,java,spring,tomcat,configuration,environments,Java,Spring,Tomcat,Configuration,Environments,我有一个基于Spring的web应用程序在tomcat容器中运行,我想维护两个不同的配置文件: src/main/resources/default.properties:包含开发、集成测试以及未设置其他属性时的默认值 …/tomcat/conf/app.properties:在不同的环境中具有不同的内容,应该覆盖default.properties 我有一个spring配置,当应用程序在tomcat中运行时,它可以正常工作 app-context.xml 当在当前上下文中未设置catali

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

  • src/main/resources/default.properties:包含开发、集成测试以及未设置其他属性时的默认值
  • …/tomcat/conf/app.properties:在不同的环境中具有不同的内容,应该覆盖default.properties
我有一个spring配置,当应用程序在tomcat中运行时,它可以正常工作

app-context.xml


当在当前上下文中未设置catalina.home属性时,是否有任何方法告诉Spring忽略位置文件:${catalina.home}/conf/app.properties?

您可以设置
忽略未找到的资源
来解决此问题

试试这个:

<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中的代码片段:

<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>

由于您有一个上下文,其中某些PP值可能无法解析,因此您可以尝试对属性使用一个
默认变量

file:${catalina.home:}/conf/app.properties
在这种情况下,如果没有
catalina.home
属性,例如在
System.getProperties()
中,路径前缀将解析为空字符串,我想Tomcat就是这样


在这之后,
ignore resource not found=“true”
完成了这项工作。

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


在“tomcat”或“web”配置文件上运行tomcat swith时,将app.properties加载到该配置文件中。

尝试使用
PP默认值
文件:${catalina.home:}/conf/app.properties
@ArtemBilan,这实际上起了作用,但我无法将您的评论标记为答案。你能把它作为答案贴出来吗?完成了。谢谢您的确认!我不确定,beucase从未满足类似的要求:-)。设置了“忽略未找到的资源”和“忽略不可解析”,但当缺少的属性是位置的一部分时,这似乎没有帮助。事实证明,空的默认值确实有帮助
<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>
file:${catalina.home:}/conf/app.properties