如何在util:properties中使用属性文件?

时间:2014-04-02 11:08:42

标签: java spring properties

我必须使用spring util:properties,但是,我需要这样的东西:

if propertyFile x exists, use x, otherwise, use y.

你能告诉我如何获得这个吗?

2 个答案:

答案 0 :(得分:0)

可能这是半解决方案! Spring可以加载通配符资源。请参阅spring <util:properties /> with wildcards

通过这种方式,您可以将文件命名为: x-config.properties y-config.properties

<bean id = "config" 
    class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name = "locations" 
        value = "classpath*:somefolder/*-config.properties" />
</bean>

如果x和y都存在,则它们都被加载。

答案 1 :(得分:0)

实际上,有一个选项ignoreResourceNotFound,但它不适用于命名空间组件。您必须直接使用PropertiesFactoryBean

<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations" value="y.properties, x.properties"/>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

如果您的x.properties不存在,则会被忽略,y.properties的属性将保留。

如果x.properties存在并且它包含与keys相同的y.properties,则会覆盖y.properties的{​​{1}},因为所有locations都会逐个加载