0

在我部署在 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 中,结果都一样。

有什么建议么?谢谢。

4

1 回答 1

2

不要在 web.xml 中列出 xml 文件,而是尝试将它们导入applicationContext.xml

<import resource="classpath*:springFile1.xml"/>

更新:您似乎正在重新定义子 xml 中的占位符配置器,从而覆盖来自父级的配置器,该配置器指定目标属性文件。从子 xml 中删除配置器声明。

于 2011-02-16T20:14:08.740 回答