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 spring:如何定义属性文件位置优先级?_Java_Spring_Spring Mvc - Fatal编程技术网

Java spring:如何定义属性文件位置优先级?

Java spring:如何定义属性文件位置优先级?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我们需要实现以下行为来解析spring项目中的属性文件(比如abc.properties): 1尝试查找与我的jar文件相邻的abc.properties 2如果在jar文件旁边找不到文件abc.properties,请在名为configs的文件夹中搜索它 我们如何使用spring PropertyPlaceHolderConfigure实现上述功能 用于基于XML的配置 对于基于注释的配置,您可以将以下注释添加到任何@configuration文件中 有关详细信息: 关键是将ignoreso

我们需要实现以下行为来解析spring项目中的属性文件(比如abc.properties):
1尝试查找与我的jar文件相邻的abc.properties
2如果在jar文件旁边找不到文件abc.properties,请在名为configs的文件夹中搜索它

我们如何使用spring PropertyPlaceHolderConfigure实现上述功能

用于基于XML的配置

对于基于注释的配置,您可以将以下注释添加到任何
@configuration
文件中

有关详细信息:


关键是将
ignoresourcenotfound
属性设置为
true

使用
PropertyPlaceHolderConfigure
的示例:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:abc.properties</value>
            <value>file:/path-to-file/abc.properites</value>
        </list>
    </property>
</bean>
<context:property-placeholder locations="classpath:abc.properties,file:/some/folder/path/override.properites"/> 
@PropertySource({
    "classpath:abc.properties",
    "file:/some/folder/path/override.properites" //This will override values with same keys as in abc.properties
})
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:abc.properties</value>
            <value>file:/path-to-file/abc.properites</value>
        </list>
    </property>
</bean>
@Configuration
@PropertySource(value = { "classpath:abc.properties", "file:/path-to-file/abc.properties" }, ignoreResourceNotFound = true)
class MyConfig {
    ...
}