我正在尝试使用属性占位符加载一些属性文件,并且我希望使用系统属性指定其中一个文件的名称,这样我就可以根据应用程序运行的环境加载不同的文件。
最初,我尝试了以下几种方法:
<context:property-placeholder location="classpath:environment_common.properties,classpath:environment_${app_env}.properties" />我验证了系统属性(app_env)设置正确(例如,"bar"),但是Spring加载了错误的文件(例如,environment_foo.properties)。
接下来我尝试使用SpEL:
<context:property-placeholder
location="#{ 'classpath:environment_common.properties,classpath:environment_'.concat(systemProperties['app_env'] }.properties) }" />但是看起来context:property-placeholder不支持SpEL:
java.io.FileNotFoundException: Could not open ServletContext resource [/#{'classpath:environment_common.properties]看起来好像context:property-placeholder有自己的解析器来寻找逗号来分隔多个属性文件,但它并不是首先将值传递给SpEL来计算它。
我应该如何使用context:property-placeholder,或者我应该直接绕过它而直接使用PropertyPlaceHolderConfigurer?
发布于 2011-08-05 12:39:53
我从未尝试在属性占位符元素中直接使用SpEL。不过,似乎有a bug为它提交了申请。作为一种相当简单的解决方法:
<context:property-placeholder properties-ref="props" />
<util:properties id="props" location="#{ your expression here }"/>发布于 2012-08-02 04:51:41
我今天遇到了这个问题。以下是我的解决方案:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="classpath:#{(T(java.lang.System).getProperty('my.property', 'development.properties'))}"/>
</bean> 我没有使用预定义的变量systemProperties,但假设您可以这样做。
https://stackoverflow.com/questions/6900259
复制相似问题