Spring上下文:property-placeholder忽略未找到的资源

时间:2013-09-09 09:00:00

标签: spring properties config

我想要一些资源但如果遗失了就忽略其他资源......如何做到这一点?我认为我只能这样做

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

这会影响那里列出的每个配置。

//编辑 这是一个有效的例子

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

2 个答案:

答案 0 :(得分:8)

为所需的依赖项添加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" />

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

答案 1 :(得分:0)

我想你也可以这样添加:

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