问题描述
我正在使用PropertyPlaceholderConfigurer和Tomcat& ContextLoaderListener的。
I am using PropertyPlaceholderConfigurer with Tomcat & ContextLoaderListener.
这是有效的(硬件编码属性文件的名称):
This works (with the name of the properties file hardcoded):
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/test.properties"/>
</bean>
但是(使用$ {env}替换属性文件的名称):
But this (with the name of the properties file replaced with ${env}):
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/${env}.properties"/>
</bean>
[Thread-2] 15:50:16 ERROR ContextLoader - 上下文初始化失败
org .springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常是java.io.FileNotFoundException:类路径资源[properties / $ {env} .properties]无法打开,因为它不存在
[Thread-2] 15:50:16 ERROR ContextLoader - Context initialization failedorg.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [properties/${env}.properties] cannot be opened because it does not exist
我知道文件是因为它在我硬编码时起作用。
I know the file is there since it works when I hardcode it.
我在启动Tomcat并设置环境变量时尝试使用-Denv = test。我使用自己的main方法而不是ContextLoaderListener在Tomcat之外工作。
I have tried using -Denv=test when starting Tomcat and setting an environment variable. I have the same thing working outside of Tomcat using my own main method instead of ContextLoaderListener.
我可能做错了什么?我可以使用context.xml或web.xml中的条目而不是-Denv = test来实现相同的功能吗?
What might I be doing wrong? Can I achieve the same thing using an entry in context.xml or web.xml instead of -Denv=test?
谢谢
PS我也试过:
<bean id="initialcorePropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
<property name="searchSystemEnvironment">
<value type="boolean">true</value>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="corePropertyConfigurer" depends-on="initialcorePropertyConfigurer" lazy-init="true"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/${env}.properties" />
</bean>
但我得到同样的错误。
推荐答案
您不能在PropertyPlaceholderConfigurer定义中使用属性占位符。鸡肉和鸡蛋。
You can't use a property placeholder in your PropertyPlaceholderConfigurer definition. Chicken-and-the-egg.
但是你可以使用#{systemProperties ['env']}
或者您可以继承 PropertyPlaceholderConfigurer
并覆盖 setLocation()
处理占位符。
Or you could subclass PropertyPlaceholderConfigurer
and override setLocation()
to handle a placeholder.
这篇关于PropertyPlaceholderConfigurer与Tomcat& ContextLoaderListener的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!