spring同时集成redis和mongodb时遇到多个资源文件加载的问题

这两天平台中集成redis和mongodb遇到一个问题

单独集成redis和单独集成mongodb时都可以正常启动程序,但是当两个同时集成进去时就会报以下问题

Could not resolve placeholder 'mongo.port' in string value "${mongo.port}

百思不得解后,经多方搜集查证,终于找到问题原因。

在spring的xml配置文件中当有多个*.properties文件需要加载时。

应该这样使用使用

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:mongodb.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

或者

<context:property-placeholder location="classpath*:redis.properties" ignore-unresolvable="true" />

但是 ignore-unresolvable="true" 和 <property name="ignoreUnresolvablePlaceholders" value="true" /> 这两个属性值必须为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如果提供一个警告就好了)。

原文章中提到最后是把所有的资源文件中的资源放在一起加载

如下:

#mongo的资源属性
mongo.host=192.168.111.230
mongo.port=40000
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1500
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=1500
mongo.slaveOk=true
mongo.write.number=1
mongo.write.timeout=0
mongo.write.fsync=true mongo.dbname=test #redis的资源属性
redis.host=192.168.111.225
redis.port=6379
redis.pass= redis.maxIdle=300
redis.maxTotal=600
redis.minIdle=100

但是本人认为这样加载不利于系统的拆分,耦合较高。因此本人推荐还是使用单独加载每个子系统自己的资源文件最好,如:

#mongo加载资源文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:mongodb.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>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

只要保证ignoreUnresolvablePlaceholders都为true,或这最后一个加载的为false,之前的都为true即可。

参考地址:http://www.myexception.cn/database/1705284.html

http://www.iteye.com/topic/1131688

spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX的更多相关文章

  1. spring错误<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX

    spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...

  2. Spring @Value注入值失败,错误信息提示:Could not resolve placeholder

    问题根源: @Value("${wx.app.config.appid}") public Object appid; 异常信息: Caused by: java.lang.Ill ...

  3. spring cloud config---Could not resolve placeholder 'xxx' in string value "${xxx}"

    初学SpringCloud 跟着视频写配置 前前后后检查了许久,配置代码没问题 最后发现是client项目的配置文件名有问题,不应该是application.yml 而是bootstrap.yml 那 ...

  4. spring boot 启动错误:Could not resolve placeholder

    在启动整个spring boot项目时,出现错误: Could not resolve placeholder 原因:没有指定好配置文件,因为src/main/resources下有多个配置文件,例如 ...

  5. Spring中报"Could not resolve placeholder"的解决方案

    除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderCon ...

  6. 【转】Spring项目启动报"Could not resolve placeholder"解决方法

    问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceho ...

  7. Spring项目启动报"Could not resolve placeholder"解决

    1.问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlace ...

  8. IDEA报错: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"

    运行审核流模块: 在ActivitiServiceApplication模块日志报错: Error starting ApplicationContext. To display the auto-c ...

  9. Spring中报"Could not resolve placeholder"的解决方案(引入多个properties文件)

    除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderCon ...

随机推荐

  1. oracle schema object

    Oracle supplies many PL/SQL packages with the Oracle server to extend database functionality and pro ...

  2. .net 开源 JavaScript 解析引擎

    1. Javascript .NET 地址为:http://javascriptdotnet.codeplex.com/ 使用方法: Quick Start This section provides ...

  3. 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例

    根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...

  4. Jfinal中定时器的初步探索(二)

    第一篇中增加的是程序代码的实现,本篇我们将通过配置文件进行定时器的配置,减少代码量,提高灵活性. 1.需要用到的文件:quartz.properties,据说这个文件如果没有的话,按默认的走,结果布署 ...

  5. ajax两种不同方式的不同结果

    function upLoadAlterData(){ $("#form_main").ajaxSubmit({ url:"XX", cache:false, ...

  6. samba配置只读共享

    编辑smb.conf 1.在[global]中 找到 security = 将其改为 security = share 2. 在文件中加入自定义的共享目录 [attachment] path=/dat ...

  7. DBA常用SQL之数据库基础信息

    第一部分: 1. 查看oracle最大连接数 sql>show parameter processes #最大连接数 2. 修改最大连接数 sql>alter system set pro ...

  8. Linux下Memcached-1.4.10安装

    memcache是一款流行的缓存产品,它分为两个部分:一个是运行在服务器端的memcached进程,一个是在客户端进行调用获取缓存中数据客户端,例如比较常用的PHP客户端.这里,记录一下安装服务器端的 ...

  9. javascript util.js

    //根据Id获得页面元素 function $(para) {    return document.getElementById(para);} //创建一个新的元素function createE ...

  10. mysql 获取设置环境变量

    mysql 获取环境变量 show global variables; 获取指定环境变量 show global variables like '%timeout'; 设置环境变量 set globa ...