在我部署在 tomcat 环境中的 webapp 中,我有一个包含 placeHolder 的 spring 配置文件,例如 ${myurl}。为了替换占位符,我在包含 PropertyPlaceholderConfigurater bean 的 WEB-INF 目录中创建了 applicationContext.xml,并将其位置设置为也在 WEB-INF 目录中的属性文件。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="placeHolderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/my.properties"/>
</bean>
<import resource="classpath*:META-INF/springFile1.xml"/>
</beans>
然后在 web.xml 中,我指定上下文:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml
</param-value>
</context-param>
在 WEB-INF/my.properties myurl=http://www.google.com
这是 springFile1.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
</bean>
<bean id="mycache" class="com.abc.MyURL"
init-method="start" destroy-method="stop">
<constructor-arg value="${myurl}" />
</bean>
</beans>
我不断得到未解析的值 ${myurl}
我尝试将属性文件放在类路径、绝对路径和WEB-INF 中,结果都一样。
有什么建议么?谢谢。