2/01/2013

Spring 3.0 "Could not resolve placeholder" solution

Except for wrong path, wrong spell, another reason can be :

There are multiple PropertyPlaceholderConfigurer or <context:property-placeholder>

For example, I have two xml config file and they are loaded at same time in the web.xml.
<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
                WEB-INF/config/spring/dao.xml,   
                WEB-INF/config/spring/service.xml  
        </param-value>  
</context-param>

Or,
In one module's config xml import other module's config xml:
<import resource="classpath:/META-INF/spring/dao.xml" />
<import resource="classpath:/META-INF/spring/service.xml" />


In these two xml files, they all have one <context:property-placeholder>:
<!-- dao.xml -->  
<context:property-placeholder location="WEB-INF/config/db/dbConnect.properties" />  
  
<!-- service.xml -->  
<context:property-placeholder location="WEB-INF/config/dfs/service.properties" />

If config like this, there must be "Could not resolve placeholder" exception.

We have to remember that, no matter we use multiple xml and load together, or use single xml.
IT IS NOT ALLOWED TO HAVE:
<context:property-placeholder location="XXX" />  
<context:property-placeholder location="YYY" />

The solution is:

In spring 3.0:

<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"  />  
<context:property-placeholder location="yyy.properties" ignore-unresolvable="true"  />

In each <context:property-placeholder> , put ignore-unresolvable="true"
Each <context:property-placeholder> need to be configured.
If any one miss this configuration, all of them will not work.


In spring 2.5:

<context:property-placeholder> does not have ignore-unresolvable attribute,it needs to be changed to PropertyPlaceholderConfigurer。
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true" /> equals to:
<bean id="xxx" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location" value="xxx.properties" />  
    <property name="ignoreUnresolvablePlaceholders" value="true" />   
</bean>

Just because of this, if you config multiple PropertyPlaceholderConfigurer, but missing ignoreUnresolvablePlaceholders, exception "Could not resolve placeholder" will be thrown.




No comments:

Post a Comment