我必须使用spring util:properties
,但是,我需要这样的东西:
if propertyFile x exists, use x, otherwise, use y.
你能告诉我如何获得这个吗?
答案 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
都会逐个加载