【发布时间】:2011-11-20 13:19:04
【问题描述】:
我将 PropertyPlaceholderConfigurer 与 Tomcat 和 ContextLoaderListener 一起使用。
这可行(使用硬编码的属性文件的名称):
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/test.properties"/>
</bean>
但是这个(将属性文件的名称替换为 ${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] 无法打开,因为它不存在
我知道该文件在那里,因为当我对其进行硬编码时它可以工作。
我尝试在启动 Tomcat 并设置环境变量时使用 -Denv=test。我使用自己的 main 方法而不是 ContextLoaderListener 在 Tomcat 之外工作。
我可能做错了什么?我可以使用 context.xml 或 web.xml 中的条目而不是 -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>
但我得到了同样的错误。
【问题讨论】: