使用spring的<context:property-placeholder location="/WEB-INF/redis.properties"/>读取properties配置文件报错 Could not resolve placeholder
项目结构
配置
启动报错
顺着这个错误向上找发现.properties文件没有全部加载, log4j.properties在web.xml中配置加载, jdbc.properties和redis.properties文件都配置在application.xml文件中,
从控制台上可以发现redis.properties文件并没有被加载
INFO: Set web app root system property: 'webapp.root' = [D:\soft\MyEclipse Professional2013workspace\.metadata\.me_tcat\webapps\zjx-springmvc\] 七月 24, 2016 12:16:29 上午 org.apache.catalina.core.ApplicationContext log INFO: Initializing log4j from [D:\soft\MyEclipse Professional2013workspace\.metadata\.me_tcat\webapps\zjx-springmvc\WEB-INF\log4j.properties] 七月 24, 2016 12:16:29 上午 org.apache.catalina.core.ApplicationContext log INFO: 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 24 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.springframework.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 not resolve placeholder 'redis.maxIdle' in string value "${redis.maxIdle}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.maxIdle' in string value "${redis.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 definition with name 'jedisPoolConfig' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Could not resolve placeholder 'redis.maxIdle' in string value "${redis.maxIdle}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.maxIdle' in string value "${redis.maxIdle}"
解决办法:
spring的xml配置文件中当有多个*.properties文件需要加载时。需要这样
方案1: 不建议使用(耦合性太大不利于拆分项目)
将多个.properties文件合到一个.properties文件中
方案2: 在<context:property-placeholder location="/WEB-INF/jdbc.properties"/>中加 ignore-unresolvable="true"
原因:
Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代 PropertyPlaceholderConfigurer了)。
而<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。
方案3: 其他加载资源文件方式
#加载jdbc资源文件 <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean> #加载redis资源文件 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value><!-- 如果是类路径下这样加载 --> <!--<value>/WEB-INF/redis.properties</value>--><!-- 如果是WEB-INFO路径下这样加载 --> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean>
只要保证ignoreUnresolvablePlaceholders都为true,或这最后一个加载的为false,之前的都为true即可。
总结:
无论是方案2还是方案三即: ignore-unresolvable="true" 和 <property name="ignoreUnresolvablePlaceholders" value="true" /> 这两个属性值必须为true
来源:https://www.cnblogs.com/YingYue/p/5699962.html