开发者

PropertyPlaceholderConfigurer just not working in tomcat, please help

开发者 https://www.devze.com 2023-02-12 01:12 出处:网络
In my webapp which is deployed in tomcat environment, I have one spring configuration file which contains placeHolder, like ${myurl}. To replace the placeholder, I created applicationContext.xml in WE

In my webapp which is deployed in tomcat environment, I have one spring configuration file which contains placeHolder, like ${myurl}. To replace the placeholder, I created applicationContext.xml in WEB-INF directory which contain开发者_C百科s a PropertyPlaceholderConfigurater bean and set its location to a properties file which also inside WEB-INF directory.

<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>

Then in web.xml, I specify the context:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml
    </param-value>
</context-param>

In WEB-INF/my.properties myurl=http://www.google.com

This is the 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>

I keep getting the unparsed value ${myurl}

I tried to put properties file in classpath, absolute path, and WEB-INF, all result the same.

Any suggestions? Thanks.


Instead of listing the xml files in web.xml, try importing them in applicationContext.xml:

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

Update: It appears you are redefining the placeholder configurer in the child xml, thus overriding the one from the parent, which specifies the target properties file. Remove the configurer declaration from the child xml.

0

精彩评论

暂无评论...
验证码 换一张
取 消