除去properites文件路徑錯誤、拼寫錯誤外,出現"Could not resolve placeholder"很有可能是使用了多個PropertyPlaceholderConfigurer或者多個<context:property-placeholder>的原因。
比如我有一個dao.xml讀取dbConnect.properties,還有一個dfs.xml讀取dfsManager.properties,然后web.xml統一load這兩個xml文件
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- WEB-INF/config/spring/dao.xml,
- WEB-INF/config/spring/dfs.xml
- </param-value>
- </context-param>
如果這兩個xml文件中分別有
- <!-- dao.xml -->
- <context:property-placeholder location="WEB-INF/config/db/dbConnect.properties" />
- <!-- dfs.xml -->
- <context:property-placeholder location="WEB-INF/config/dfs/dfsManager.properties" />
那么,一定會出"Could not resolve placeholder"。
一定要記住,不管是在一個spring文件還是在多個Spring文件被統一load的情況下,直接寫
- <context:property-placeholder location="" />
- <context:property-placeholder location="" />
是不允許的。
解決方案:
(1) 在Spring 3.0中,可以寫:
- <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"
- />
- <context:property-placeholder location="yyy.properties" ignore-unresolvable="true"
- />
注意兩個都要加上ignore-unresolvable="true",一個加另一個不加也是不行的
(2) 在Spring 2.5中,<context:property-placeholder>沒有ignore-unresolvable屬性,此時可以改用PropertyPlaceholderConfigurer。其實<context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />與下面的配置是等價的
- <bean id="隨便" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="xxx.properties" />
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- </bean>
正因為如此,寫多個PropertyPlaceholderConfigurer不加ignoreUnresolvablePlaceholders屬性也是一樣會出"Could not resolve placeholder"。