The project startup report "Could not resolve placeholder" is resolved


1. The cause of the problem:
  In addition to the wrong path and spelling of the properties file, "Could not resolve placeholder" is likely to be the reason for using multiple PropertyPlaceholderConfigurers or multiple <context:property-placeholder>.

  For example, I have a dao.xml that reads dbConnect.properties, and a dfs.xml that reads dfsManager.properties, and then web.xml uniformly loads these two xml files, which leads to the problem.

Solution:

  (1) In Spring 3.0, you can write

Xml code

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

Note that ignore-unresolvable="true" must be added to both, and it is not possible to add one without the other.



  (2) In Spring 2.5, <context:property-placeholder> does not have ignore -unresolvable property, you can use PropertyPlaceholderConfigurer instead. Actually <context:

Configuration content:
<bean id="whatever" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="xxx.properties" /> 
    <property name="ignoreUnresolvablePlaceholders" value ="true" />  
</bean> 

  Because of this, writing multiple PropertyPlaceholderConfigurers without adding the ignoreUnresolvablePlaceholders property will also result in "Could not resolve placeholder".

  Although the two are equivalent, it is estimated that everyone still likes to write more <context:property-placeholder>, after all, it is simpler. So if it is Spring 3.0, it is easier to use solution (1) directly; if it is Spring 2.5, it takes a little effort to rewrite it into PropertyPlaceholderConfigurer




code calling sequence
1.web.xml
<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/context-servlet.xml</param-value>
	</context-param>

2. The code to solve

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
    	<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>/WEB-INF/eaching.properties</value>
				<value>/WEB-INF/jdbc.properties</value>
			</list>
		</property>
	</bean>

3.<import resource="config/applicationContext-profile.xml"/>中
<beans profile="dev">
        	<context:property-placeholder location="classpath*:config/*.properties,file:${sucang.root}/WEB-INF/config/dev/*.properties,classpath*:dev/*.properties"/>
        <import resource="classpath*:dev/applicationContext-*.xml"/>
    </beans>

    <beans profile="test">
        <context:property-placeholder location="classpath*:config/*.properties,file:${sucang.root}/WEB-INF/config/test/*.properties,classpath*:test/*.properties"/>
		<import resource="classpath*:test/applicationContext-*.xml"/>
    </beans>
    
    <beans profile="prod">
    		<context:property-placeholder location="classpath*:config/*.properties,file:${sucang.root}/WEB-INF/config/prod/*.properties,classpath*:prod/*.properties"/>
		<import resource="classpath*:prod/applicationContext-*.xml"/>
    </beans>






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326085127&siteId=291194637