Read the properties profile using Spring's <context:property-placeholder location= "/web-inf/redis.properties"/> Error Could not Resolve placeholder
Project structure
Configuration
Start error
Follow this error up and find the. properties file is not fully loaded, Log4j.properties is configured to load in Web. XML, Both the jdbc.properties and Redis.properties files are configured in the Application.xml file.
From the console you can find that the Redis.properties file is not loaded
Info:set Web App root system property: ' webapp.root ' = [D:\soft\MyEclipse professional2013workspace\.metadata\.me_tcat\ Webapps\zjx-springmvc\] July 24, 2016 12:16:29 am org.apache.catalina.core.ApplicationContext loginfo:initializing log4j From [D:\soft\MyEclipse professional2013workspace\.metadata\.me_tcat\webapps\zjx-springmvc\web-inf\ Log4j.properties] July 24, 2016 12:16:29 am org.apache.catalina.core.ApplicationContext loginfo:initializing Spring Root Webapplicationcontext[info][2016-07-24 00:16:29] Org.springframework.web.context.ContextLoader.initWebApplicationContext (contextloader.java:305) Root Webapplicationcontext:initialization started [info][2016-07-24 00:16:30] Org.springframework.context.support.AbstractApplicationContext.prepareRefresh (Abstractapplicationcontext.java : 578) Refreshing Root webapplicationcontext:startup date [Sun Jul 00:16:30 CST 2016]; Root of context hierarchy [info][2016-07-24 00:16:31] Org.springframework.beans.factory.xml.XmlBeanDefinitionReaDer.loadbeandefinitions (xmlbeandefinitionreader.java:317) Loading XML bean Definitions from servletcontext resource [/ Web-inf/applicationcontext.xml] [info][2016-07-24 00:16:35] Org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties (propertiesloadersupport.java:172) Loading properties file from ServletContext resource [/web-inf/jdbc.properties] [warn][2016-07-24 00:16:35] Org.spring Framework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:546) Exception Encountered during context initialization-cancelling refresh attempt: Org.springframework.beans.factory.BeanDefinitionStoreException:Invalid Bean definition with name ' Jedispoolconfig ' defined in ServletContext resource [/web-inf/applicationcontext.xml]: Could, resolve placeholder ' redis.maxidle ' in St Ring value "${redis.maxidle}"; Nested exception is java.lang.IllegalArgumentException:Could not resolve placeholder ' redis.maxidle ' in string value "${r Edis.maxidle} " [ERROR] [2016-07-24 00:16:35] Org.springframework.web.context.ContextLoader.initWebApplicationContext (Contextloader.java : 351) Context initialization failed Org.springframework.beans.factory.BeanDefinitionStoreException:Invalid Bean Defi Nition with name ' jedispoolconfig ' defined in ServletContext resource [/web-inf/applicationcontext.xml]: Could not Resolv E placeholder ' redis.maxidle ' in string value "${redis.maxidle}"; Nested exception is java.lang.IllegalArgumentException:Could not resolve placeholder ' redis.maxidle ' in string value "${r Edis.maxidle} "
Workaround:
In the spring XML configuration file, when there are multiple *.properties files that need to be loaded. Need this
Scenario 1: Not recommended (coupling is too big for splitting projects)
Combine multiple. Properties files into a single. properties file
Scenario 2: Add ignore-unresolvable= "true" in <context:property-placeholder location= "/web-inf/jdbc.properties"/>
Reason:
The spring container uses the discovery mechanism of reflection scanning, which has a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in the detection of spring containers. The bean will stop scanning the remaining propertyplaceholderconfigurer (Spring 3.1 has used propertysourcesplaceholderconfigurer instead Propertyplaceholderconfigurer).
<context:property-placeholder/>, the namespace-based configuration, actually creates a propertyplaceholderconfigurer bean inside. in other words, the spring container allows only a maximum of one propertyplaceholderconfigurer (or <context:property-placeholder/>) to be defined, The rest will be ignored by spring (in fact, if spring provides a warning).
Scenario 3: Other ways to load resource files
#加载jdbc资源文件
<BeanClass= "Org.springframework.jdbc.datasource.DriverManagerDataSource">
<PropertyName= "Locations">
<List>
<Value>classpath:jdbc.properties</Value> </list> </Property> <property name= " Ignoreunresolvableplaceholders " Value=" true " /> </bean>
#加载redis资源文件
<Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"> <List> <value>Classpath:redis.properties</value><!--If this is the class path, load-
<!--<value>/web-inf/redis.properties</value>--><!-- If this is the Web-info path, load - </List> </ Property> < Propertyname= "Ignoreunresolvableplaceholders"value= "true" /> </Bean>
As long as the ignoreunresolvableplaceholders is guaranteed to be true, or this last load is false, the previous one is true.
Summarize:
Either scenario 2 or scenario three namely: Ignore-unresolvable= "true" and <property name= "Ignoreunresolvableplaceholders" value= "true"/> Both of these property values must be True
Could not resolve placeholder