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

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

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

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

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

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

应该这样使用使用

  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <property name="locations">
  3. <list>
  4. <value>classpath*:mongodb.properties</value>
  5. </list>
  6. </property>
  7. <property name="ignoreUnresolvablePlaceholders" value="true" />
  8. </bean>

或者

  1. <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如果提供一个警告就好了)。

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

如下:

  1. #mongo的资源属性
  2. mongo.host=192.168.111.230
  3. mongo.port=40000
  4. mongo.connectionsPerHost=8
  5. mongo.threadsAllowedToBlockForConnectionMultiplier=4
  6. mongo.connectTimeout=1500
  7. mongo.maxWaitTime=1500
  8. mongo.autoConnectRetry=true
  9. mongo.socketKeepAlive=true
  10. mongo.socketTimeout=1500
  11. mongo.slaveOk=true
  12. mongo.write.number=1
  13. mongo.write.timeout=0
  14. mongo.write.fsync=true
  15.  
  16. mongo.dbname=test
  17.  
  18. #redis的资源属性
  19. redis.host=192.168.111.225
  20. redis.port=6379
  21. redis.pass=
  22.  
  23. redis.maxIdle=300
  24. redis.maxTotal=600
  25. redis.minIdle=100

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

  1. #mongo加载资源文件
  2. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  3. <property name="locations">
  4. <list>
  5. <value>classpath*:mongodb.properties</value>
  6. </list>
  7. </property>
  8. <property name="ignoreUnresolvablePlaceholders" value="true" />
  9. </bean>
  10.  
  11. #redis加载资源文件
  12. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  13. <property name="locations">
  14. <list>
  15. <value>classpath*:redis.properties</value>
  16. </list>
  17. </property>
  18. <property name="ignoreUnresolvablePlaceholders" value="true" />
  19. </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. linux服务之iptables与firewalld

    开发语言: 服务器端:在内核中实现,无守护程序 客户端:一般是cli界面下的iptables命令 相关包:iptables-1.4.7-11.el6.x86_64 netfilter/iptables ...

  2. jQuery中怎么添加innerText、innerHtml(转)

    发现如果我在div或者其他非表单的标签中赋值,原本用普通的js就直接document.getElementById("id").innerHtml(或者其他几个)就可以了. 但是在 ...

  3. 通过命令行安装Android app

    手动安装安卓app的命令为:adb install -r C:\Users\Lihao\workspace\Appium_Demo\apps\app_F_1.3.0.apk

  4. TKinter布局之grid 网格布局

    1.由于我们的程序大多数都是矩形,因此特别适合于网格 布局,也就是 grid 布局. 2.使用 grid 布局的时候,我们使用 grid 函数,在里面指 定两个参数,用 row 表示行,用 colum ...

  5. python之路之正则表达式

    匹配格式^ 匹配字符串的开头$ 匹配字符串的结尾. 除了换行符外的所有字符[...] 用来表示一组字符,,单独列出:[amk]匹配'a','m'或'k'[^..] 不在[]中的字符:[^abc]匹配除 ...

  6. ASP.NET MVC几种找不到资源的问题解决办法

    转自:http://www.cnblogs.com/xyang/archive/2011/11/24/2262003.html 在MVC中,controller中的Action和View中的.csht ...

  7. Mysqldump参数大全(转)

    参数 参数说明 --all-databases  , -A 导出全部数据库. mysqldump  -uroot -p --all-databases --all-tablespaces  , -Y ...

  8. Eclipse的中文字体设置

    打开eclipse中文字体很小,简直难以辨认.在网上搜索发现这是由于Eclipse 用的字体是 Consolas,显示中文的时候默认太小了.解决方式有两种:一.把字体设置为Courier New  操 ...

  9. sublime安装DocBlockr注释插件

    点击sublime的菜单栏 view->show console :现在打开了控制台, 这个控制台有上下两栏, 上面一栏会实时显示sublime执行了什么插件,输出执行结果, 如果你安装的某个插 ...

  10. 重新定位svn地址的方法(windows和linux),svn switch(sw)的帮助信息

    今天公司的路由器出现问题,服务器的IP地址也跟着变了,但是原来的svn已经设置好了,现在需要更换地址 查询原地址的方法: root@jack-desktop:codes# svn info 路径: . ...