Java 如何在util:properties中使用属性文件?
我必须使用springJava 如何在util:properties中使用属性文件?,java,spring,properties,Java,Spring,Properties,我必须使用springutil:properties,但是,我需要这样的东西: if propertyFile x exists, use x, otherwise, use y. 你能告诉我怎么才能得到这个吗?可能这是一半的解决方案!Spring可以加载通配符资源。请看 通过这种方式,您可以将文件命名为:x-config.properties和y-config.properties: <bean id = "config" class = "org.springframewo
util:properties
,但是,我需要这样的东西:
if propertyFile x exists, use x, otherwise, use y.
你能告诉我怎么才能得到这个吗?可能这是一半的解决方案!Spring可以加载通配符资源。请看 通过这种方式,您可以将文件命名为: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
相同的键,则它们将覆盖y.properties
中的键,因为所有位置都将逐个加载