Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在util:properties中使用属性文件?_Java_Spring_Properties - Fatal编程技术网

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

Java 如何在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

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

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

你能告诉我怎么才能得到这个吗?

可能这是一半的解决方案!Spring可以加载通配符资源。请看

通过这种方式,您可以将文件命名为:x-config.propertiesy-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
中的键,因为所有
位置都将逐个加载