Spring的PropertyPlaceholderConfigurer强制使用默认值的坑
1.问题
dubbo client配置:
<dubbo:reference id="channelCustomerClient" interface="com.gttown.crm.channel.service.ChannelCustomerService"
timeout="60000" check="false" filter="clientFilter" retries="0" validation="false"
url="${dubbo.url.channel:#{null}}"/>
dubbo.properties:
zipkin.url=http://zipkin.dev.great-tao.com
dubbo.application=gttown-user-web
dubbo.register=false
dubbo.port=20880
dubbo.url.channel=dubbo://127.0.0.1:20881
dubbo配置时,预期效果:url="${dubbo.url.channel:#{null}}" 会先读取配置文件dubbo.url.channel的值如果有值则读取,若配置文件无该值则用默认值null。
但是事实上无论dubbo.properties配置文件是否有dubbo.url.channel。url的值都会强制使用默认值null。
PropertyPlaceholderConfigurer配置参考。 (具体参考http://elim.iteye.com/blog/2387138)
2.原因
spring.xml 里的PropertyPlaceholderConfigurer配置:
<bean id="placeholderConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:/*.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
db-user.xml 里的PropertyPlaceholderConfigurer配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="-1"/>
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
spring.xml的PlaceholderConfigurer配置扫描了classpath下的全部文件,但是加载顺序order使用的是默认值。
db-user.xml的PlaceholderConfigurer配置只扫描了classpath下的db.properties,加载顺序order为-1最后加载。
url="${dubbo.url.channel:#{null}}"配置时由于db-user.xml的PlaceholderConfigurer后加载,而该PlaceholderConfigurer只扫描了db.properties。该文件没有dubbo.url.channel的值,导致url使用默认值null。
3.解决
方案1:
spring.xml 里的PropertyPlaceholderConfigurer配置加上<property name="order" value="-1"/>
建议:扫面范围越大的PropertyPlaceholderConfigurer越后面加载。
方案2:
删除db-user.xml 以及以其他配置文件里的精确扫描指定文件的PropertyPlaceholderConfigurer配置。方案3:
spring.xml 里的PropertyPlaceholderConfigurer配置,修改分隔符(默认分隔符为:)<property name="valueSeparator" value="&"/>
使用& 作为分隔符。
dubbo-clien.xml配置修改为
url="${dubbo.url.channel&#{null}}
Spring的PropertyPlaceholderConfigurer强制使用默认值的坑的更多相关文章
- spring mvc 绑定参数据默认值,是否必传,(RequestParam(value="id",defaultValue="1",required=true) )
@RequestMapping(value = "/detail", method = RequestMethod.GET) public String newDetail(@Re ...
- 【oracle】关于创建表时用default指定默认值的坑
刚开始学create table的时候没注意,学到后面发现可以指定默认值.于是写了如下语句: 当我查询的时候发现,查出来的结果是这样的.. 很纳闷有没有,我明明指定默认值了呀,为什么创建出来的表还是空 ...
- 关于spring的注解方式注入默认值(转) -- 首字母小写
1.是首字母小写 比如 UserAction对应的id是userAction 可以通过ApplicationContext 对象的act.getBean("userAction") ...
- spring 配置文件属性设置默认值以及读取环境变量值
在 Spring 中为 javabean 注入属性文件中的属性值一般人都知道的,可以通过 org.springframework.beans.factory.config.PropertyPlaceh ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...
- spring @Value 设置默认值
@Value("${spring.value.test}") private String value; 如果配置文件中没有设置 spring.value.test 在启动的时候讲 ...
- 给Spring的placeholder设置默认值
问题:使用Spring时,可以方便地通过placeholder的形式${key}将key对应的properities定义value,注入到Bean中.但是如果在properities文件中,没有对ke ...
- spring中使用@Value设置全局变量默认值
前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
随机推荐
- Get Docker CE for CentOS
To get started with Docker CE on CentOS, make sure you meet the prerequisites, then install Docker. ...
- Oracle中的instr()函数
一.instr()函数 1.语法:instr(sourceString,destString,start,appearPosition) sourceString代表源字符串; destString代 ...
- go语言nsq源码解读九 tcp和http中channel、topic的增删
通过前面多篇文章,nsqlookupd基本已经解读完毕了,不过在关于channel和topic的增删上还比较模糊,所以本篇将站在宏观的角度来总结一下,tcp.go和http.go两个文件中关于chan ...
- BZOJ_4773_负环_倍增弗洛伊德
BZOJ_4773_负环 Description 在忘记考虑负环之后,黎瑟的算法又出错了.对于边带权的有向图 G = (V, E),请找出一个点数最小的环,使得 环上的边权和为负数.保证图中不包含重边 ...
- BZOJ_1486_[HNOI2009]最小圈_01分数规划
BZOJ_1486_[HNOI2009]最小圈_01分数规划 Description Input Output Sample Input 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 ...
- 【爆料】-《悉尼科技大学毕业证书》UTS一模一样原件
☞悉尼科技大学毕业证书[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归 ...
- 关于” 记一次logback传输日志到logstash根据自定义设置动态创建ElasticSearch索引” 这篇博客相关的优化采坑记录
之前写过一篇博客是关于记录日志的简单方式的 主要就是 应用->redis->logstash->elasticsearch 整个流程的配置方法和过程的 虽然我们部分线上应用使用 ...
- MIP 问题解决方案大全(2018-06更新)
在 MIP 推出后,我们收到了一些站长的疑问.现将常见问题整理出来,帮助大家了解 MIP 的知识. 一.MIP 认知类问题 二.改造前准备 三.前端改造,组件使用 四.提交生效 五.MIPCache ...
- python基于selenium实现自动删除qq空间留言板
py大法好,让你解放双手. 脚本环境 python环境,selenium库,Chrome webdriver驱动等. 源码 # coding=utf-8 import datetime import ...
- 深入理解OkHttp源码(一)——提交请求
本篇文章主要介绍OkHttp执行同步和异步请求的大体流程.主要流程如下图: 主要分析到getResponseWidthInterceptorChain方法,该方法为具体的根据请求获取响应部分,留着后面 ...