Java spring:如何定义属性文件位置优先级?
我们需要实现以下行为来解析spring项目中的属性文件(比如abc.properties):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
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 {
...
}