Could not resolve placeholder

使用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

时间: 2024-11-07 16:31:35

Could not resolve placeholder的相关文章

Could not resolve placeholder &#39;spring.security.mapping.directory&#39; in string value &quot;${spring.security

1.错误描述 2015-09-20 22:30:25 [main] WARN org.springframework.context.support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanDefinitionStoreException: In

Could not resolve placeholder &#39;master.jdbc.url&#39; in string value &quot;${master.jdbc.url}&quot;

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'masterDataSource' defined in file [C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\xinghe-mycenter-web\WEB-INF\classes\spri

Could not resolve placeholder &#39;jdbc.username&#39; in string value &quot;${jdbc.username}&quot;

1.错误描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource'

【转】Could not resolve placeholder 解决方案

spring 配置加载properties文件的时候,报 Could not resolve placeholder 错误. 经过仔细查找,排除文件路径,文件类容错误的原因,经过查找相关资料,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>的原因或者是多个PropertyPlaceholderConfigurer

spring错误:&lt;context:property-placeholder&gt;:Could not resolve placeholder XXX in string value XXX

spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是当两个同时集成进去时就会报以下问题 Could not resolve placeholder 'mongo.port' in string value "${mongo.port} 百思不得解后,经多方搜集查证,终于找到问题原因. 在spring的xml配置文件中当有多个*.properties文

websphere启动报/WEB-INF/applicationContext-orm- hibernate.xml]: Could not resolve placeholder &#39;hibernate.hbm2ddl.auto&#39; in string value &quot;${hibernate.hbm2ddl.auto}&quot;

启动websphere错误: [17-11-20 16:19:52:834 CST] 0000008e webapp I com.ibm.ws.webcontainer.webapp.WebApp log SRVE0292I: Servlet 消息 - [market_war#market.war]:.Initializing Spring root WebApplicati onContext [17-11-20 16:19:53:361 CST] 0000008e SystemOut O 2

Could not resolve placeholder &#39;driver&#39; in string value &quot;${driver}

可能是因为配置文件jdbc.properties 和xml文件不一致导致的,比如 jdbc.properties里面写的是 但是xml文件里面写 导致了取值错误,两边一致即可. Could not resolve placeholder 'driver' in string value "${driver} 原文地址:https://www.cnblogs.com/ljsn/p/8656493.html

Could not resolve placeholder&#39;XXX&#39; in string value &quot;XXXX&quot;

练习SSM项目的demo中遇到一个问题,我在applicationContext.xml中使用了<context:property-placeholder location="classpath:jdbc.properties"/>,我还想再引入另外一个resource.properties,resource.properties中的key我在controller层引用,用的是@Value(value="${type}")注解,总是出现如题的问题,从网上

【转】Spring项目启动报&quot;Could not resolve placeholder&quot;解决方法

问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>的原因. 比如我有一个dao.xml读取dbConnect.properties,还有一个dfs.xml读取dfsManager.properties,然后web.xml统一load这两个xml文件,就