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. 03-Java String字符串详解

    1.Java字符串String A.实例化String字符串:直接赋值(更合理一些,使用较多).使用关键字new. B.String内容的比较 // TODO Auto-generated metho ...

  2. Jmeter使用

    好久没有试过Jmeter了,下载个新版本试试,顺便温习一下. 1. 如何修改JMeter语言环境 在菜单栏中通过“选项”–“选择语言”选了英文后,下次登录JMeter,还是显示的中文,修改语言无效.关 ...

  3. EnterpriseLibrary之Caching应用

    http://blog.csdn.net/linux7985/article/details/6239433 http://cache.baiducontent.com/c?m=9f65cb4a8c8 ...

  4. 程序员书单_java专项进阶篇

    JDBC API数据库编程实作教材 http://download.csdn.net/detail/shenzhq1980/9145715 Java事务设计模式 http://download.csd ...

  5. 【python】filter,map,reduce和lambda函数介绍

    filter(function, iterable)map(function, iterable)reduce(function, sequence) filter将 function依次作用于ite ...

  6. Windows 安装程序无法将 Windows 配置为在此计算机的硬件上运行

    遇到这个问题是用辅助工具(WinNTSetup3.exe)进行的安装,重启后就就遇到“Windows 安装程序无法将 Windows 配置为在此计算机的硬件上运行” 解决:在WIN PE 下挂载安装光 ...

  7. Android中的布局优化方法

    http://blog.csdn.net/rwecho/article/details/8951009 Android开发中的布局很重要吗?那是当然.一切的显示样式都是由这个布局决定的,你说能不重要吗 ...

  8. Perl参考函数

    这是标准的Perl解释器所支持的所有重要函数/功能的列表.在一个函数中找到它的详细信息. abs - 绝对值函数 accept - 接受传入的socket连接 alarm - 调度一个SIGALRM ...

  9. 解密:wp-includes/load.php

    描述:定义加载 WP 所需要的函数.1)wp_unregister_GLOBALS(),关闭’GLOBALS’, ‘_GET’, ‘_POST’, ‘_COOKIE’, ‘_REQUEST’, ‘_S ...

  10. 黄聪:wordpress如何使用get_avatar禁止调用gravatar头像,替换为自定义头像

    add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 ); function my_custom_avatar( $avatar, $id_or_ ...