[英]Override Spring properties with VM options
我想实现以下行为:
我使用Spring ResourceBundleMessageSource
属性文件,其属性如下:
my.prop=42
现在我想用VM选项覆盖属性,如下所示:
-Dmy.prop=13
我怎样才能以简单透明的方式做到这一点?
谢谢!
仅将MessageSource用于语言字符串。
使用属性文件作为属性。
现在,您可以使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer加载属性文件
在XML中,不使用SYSTEM_PROPERTIES_MODE_FALLBACK的默认值,而是将其设置为使用SYSTEM_PROPERTIES_MODE_OVERRIDE。
您现在可以使用@Value注释加载道具,VM选项将覆盖您在属性文件中设置的值
例:
因此,请调用MessageSource messages.properties
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="fileEncodings" value="UTF-8"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
并调用属性文件misspiggy.properties(或其他:))
<bean id="propertyLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="fileEncoding" value="UTF-8"/>
<property name="locations">
<list>
<value>classpath:misspiggy.properties</value>
</list>
</property>
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.