我们在Spring配置(details here)中使用PropertyPlaceholderConfigurer来使用java属性。
例如:
<foo name="port">
<value>${my.server.port}</value>
</foo>
我们想要添加一个额外的属性,但是有一个分布式系统,其中现有的实例都可以使用默认值。有没有一种方法可以避免更新我们所有的属性文件,当没有定义覆盖属性值时,在Spring配置中指定一个默认值?
发布于 2010-03-29 07:07:57
Spring3支持${my.server.port:defaultValue}
语法。
发布于 2014-06-27 00:40:57
有一个鲜为人知的特性,这使得这一点变得更好。您可以使用可配置的默认值,而不是硬编码的默认值,下面是一个示例:
config.properties:
timeout.default=30
timeout.myBean=60
context.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>config.properties</value>
</property>
</bean>
<bean id="myBean" class="Test">
<property name="timeout" value="${timeout.myBean:${timeout.default}}" />
</bean>
要在使用默认设置的同时仍能在以后轻松覆盖,请在config.properties中执行以下操作:
timeout.myBean = ${timeout.default}
发布于 2012-10-02 23:32:41
<foo name="port">
<value>${my.server.port:8088}</value>
</foo>
应该可以将8088作为默认端口
另请参阅:http://blog.callistaenterprise.se/2011/11/17/configure-your-spring-web-application/
https://stackoverflow.com/questions/2513484
复制相似问题