spring错误<context:property-placeholder>: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文件需要加载时。
应该这样使用使用
<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即可。
spring错误<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX的更多相关文章
- spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX
spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...
- Spring @Value注入值失败,错误信息提示:Could not resolve placeholder
问题根源: @Value("${wx.app.config.appid}") public Object appid; 异常信息: Caused by: java.lang.Ill ...
- spring cloud config---Could not resolve placeholder 'xxx' in string value "${xxx}"
初学SpringCloud 跟着视频写配置 前前后后检查了许久,配置代码没问题 最后发现是client项目的配置文件名有问题,不应该是application.yml 而是bootstrap.yml 那 ...
- spring boot 启动错误:Could not resolve placeholder
在启动整个spring boot项目时,出现错误: Could not resolve placeholder 原因:没有指定好配置文件,因为src/main/resources下有多个配置文件,例如 ...
- Spring中报"Could not resolve placeholder"的解决方案
除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderCon ...
- 【转】Spring项目启动报"Could not resolve placeholder"解决方法
问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceho ...
- Spring项目启动报"Could not resolve placeholder"解决
1.问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlace ...
- 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 ...
- Spring中报"Could not resolve placeholder"的解决方案(引入多个properties文件)
除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderCon ...
随机推荐
- unity中的欧拉角
unity中欧拉角用的是heading - pitch -bank系统(zxy惯性空间旋转系统):当认为旋转顺序是zxy时,是相对于惯性坐标系旋转.当认为旋转顺序是yxz时,是相对于物体坐标系旋转. ...
- 简单配置IIS 以及web service 实现js跨域
因为浏览器的安全模型,js 是不能跨域的. 解决的方法有以下几种: 1. 使用代理服务转发 2. 目前服务器添加:Access-Control-Allow-Origin 3. 使用jsonp 4. 使 ...
- 第一篇帖子,就弄个JS动态公告浏览吧,直接上代码
.scroll{ margin-left:45px; margin-top:-150px; height:100px; width:300px;} <script lan ...
- Git服务器搭建及SSH无密码登录设置
在Git服务器中建立一个git帐号,用于多人使用. adduser git输入此命令后,会在/home/下建立一个git文件 /home/git 下建立.ssh目录(注意,是.ssh..有个点!) c ...
- Android 三种方式实现自定义圆形页面加载中效果的进度条
转载:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76872 一.通过动画实现 定义res/anim/loading.xml如 ...
- mac安装IE浏览器
1.首先得下载一个WineBottler for mac. 2.下载完毕之后,打开dmg文件后将WineBottler Combo里面的Wine和WineBottler这两个程序拖拉进应用程序. 3. ...
- Maven 如何为不同的环境打包 —— 开发、测试和生产环境
在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置,那每次 ...
- SQL集合运算参考及案例(二):树形节点数量逐级累计汇总
问题描述: 我们经常遇到这样一个问题,类似于面对一个树形结构的物料数据,需要将库存中每一种物料数量汇总到物料上展示出来:或者说组织机构是一棵树,我们需要统计每一个节点上的人员数量(含下级节点的累计数量 ...
- ASP.NET MVC 中的ViewData与ViewBag
在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...
- Struts 1.3(第一例) - Login
本想跳过直接学Struts 2的,想想,还是先学Struts 1,万一到时去那个公司,人家用的是1,那还是要学,以及了解下1与2的区别在哪里. 上例子,很简单的一个网上login例子,再思考下Stru ...