Spring 上下文:属性占位符忽略未找到的资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18694781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:13:21  来源:igfitidea点击:

Spring context:property-placeholder ignore resources not found

springpropertiesconfig

提问by kboom

I'd like to have some resources required but ignore other one if its missing... How to do that? As I see it I can only do

我想要一些资源,但如果缺少其他资源,则忽略其他资源......如何做到这一点?正如我所见,我只能做

<context:property-placeholder
    ignore-resource-not-found="true" 
location="required.properties, not-required-override.properties" />

Which affects every config listed mentioned there.

这会影响那里提到的每个配置。

// EDIT This is a working example

// 编辑 这是一个工作示例

<bean id="requiredProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:database.properties</value>
            <value>classpath:storage.properties</value>
            <value>classpath:log4j.properties</value>
            <value>classpath:mailing.properties</value>
        </list>
    </property>
</bean>

<context:property-placeholder
    properties-ref="requiredProperties" ignore-resource-not-found="true"
    location="file:${user.dir}/config/cvnizer.properties" />

回答by M. Deinum

Add a PropertiesFactoryBeanelement for the required dependencies and simply wire the properties to the <context:property-placeholder />.

PropertiesFactoryBean为所需的依赖项添加一个元素,只需将属性连接到<context:property-placeholder />.

<bean id="requiredProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations" value="classpath:file1.properties,classpath:file2.properties" />
</bean>

<context:property-placeholder properties-ref="requiredProperties" ignore-resource-not-found="true" location="not-required-override.properties" />

The properties will me merged at runtime so you can still override when a properties file is read.

这些属性将在运行时合并,因此您仍然可以在读取属性文件时进行覆盖。

回答by Achilles Ram Nakirekanti

i think you can add like this also :

我想你也可以这样添加:

<context:property-placeholder location="/WEB-INF/properties/config.properties" order="1" ignore-unresolvable="true"/>