gpt4 book ai didi

java - Spring属性文件设置默认值

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:37:45 27 4
gpt4 key购买 nike

我的 war 文件外有一个属性文件,系统管理员使用它来关闭某些系统功能。它在我的本地机器上运行良好,但是当我们部署到开发环境时,属性文件没有上传,应用程序无法启动。我想知道是否有一种方法可以在我的 applicationContext 中为通常来自属性文件的值声明默认值。

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

<util:properties id="myProperties" location="file:${catalina.home}/webapps/myProperties.properties"/>

只要我们记得将属性文件放在正确的位置,这就可以正常工作。有没有办法声明默认值,或者如果找不到该文件,也许可以从不同的文件中读取?

谢谢

最佳答案

而不是使用 <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>

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

关于java - Spring属性文件设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6375016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com