【发布时间】:2014-04-02 11:08:42
【问题描述】:
我必须使用 spring util:properties,但是,我需要这样的东西:
if propertyFile x exists, use x, otherwise, use y.
你能告诉我如何获得这个吗?
【问题讨论】:
标签: java spring properties
我必须使用 spring util:properties,但是,我需要这样的东西:
if propertyFile x exists, use x, otherwise, use y.
你能告诉我如何获得这个吗?
【问题讨论】:
标签: java spring properties
也许这是一半的解决方案! 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 都存在,则它们都被加载。
【讨论】:
其实有一个选项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 存在并且它包含与y.properties 相同的keys,它们会覆盖来自y.properties 的那些,因为所有locations 都是分别加载的。
【讨论】: