Spring属性文件设置默认值

blo*_*824 8 java configuration spring properties

我的war文件之外有一个属性文件,系统管理员使用该文件来关闭某些系统功能.它已经在我的本地机器上工作得很好但是当我们部署到开发环境时,没有上传属性文件并且应用程序无法启动.我想知道是否有办法在applicationContext中为通常来自属性文件的值声明默认值.

我目前有这个阅读属性文件:

<util:properties id="myProperties" location="file:${catalina.home}/webapps/myProperties.properties"/>
Run Code Online (Sandbox Code Playgroud)

只要我们记得将属性文件放在正确的位置,这样就可以正常工作.有没有办法声明默认值,或者如果找不到这个文件,可能会从另一个文件读取?

谢谢

sou*_*ica 11

而不是使用<util:properties>使用PropertiesFactoryBeansetIgnoreResourceNotFound=true.

例如:

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
   <property name="ignoreResourceNotFound"><value>true</value></property>
   <property name="locations">
      <list>
        <value>classpath:default.properties</value>
        <value>file:${catalina.home}/webapps/myProperties.properties</value>
      </list>
   </property>
</bean> 
Run Code Online (Sandbox Code Playgroud)

请注意列出的文件的顺序很重要.以后文件中的属性将覆盖之前的属性.