在 Spring EL 中使用 context:property-placeholder 位置
我正在尝试使用属性占位符来加载一些属性文件,并且我想使用系统属性指定其中一个文件的名称,以便我可以根据应用程序运行的环境加载不同的文件。
最初我尝试了以下操作:
<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
?
I'm trying to use a property-placeholder to load some properties files, and I want to specify the name of one of the files using a system property, so that I can load different files based on the environment that my app runs in.
Initially I tried the following:
<context:property-placeholder location="classpath:environment_common.properties,classpath:environment_${app_env}.properties" />
I validated that the system property (app_env) is set correctly (e.g., "bar"), but Spring is loading the wrong file, (e.g., environment_foo.properties).
I tried using SpEL next:
<context:property-placeholder
location="#{ 'classpath:environment_common.properties,classpath:environment_'.concat(systemProperties['app_env'] }.properties) }" />
But it appears that context:property-placeholder
doesn't support SpEL:
java.io.FileNotFoundException: Could not open ServletContext resource [/#{'classpath:environment_common.properties]
It looks as though context:property-placeholder
has its own parser looking for commas to delimit multiple property files, but its not first passing the value to SpEL to evaluate it.
How should I be using context:property-placeholder
, or should I just bypass it and use PropertyPlaceHolderConfigurer
directly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未尝试过直接在属性占位符元素中使用 SpEL。不过,似乎有一个错误为此提交。作为一个相当简单的解决方法:
I've never tried to use SpEL directly in a property-placeholder element. There appears to be a bug filed for it, though. As a rather simple workaround:
我今天遇到了这个问题。这是我的解决方案:
我没有使用预定义的变量 systemProperties 但假设您可以如果您愿意的话。
I faced this problem today. Here is my solution:
I didn't use the predefined variable systemProperties but suppose that you could if you wanted to.