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做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
随机推荐
- 附录C--拉格朗日对偶性
1.原始问题 假设$f(x)$,$c_i(x)$,$h_j(x)$是定义在$R^n$上的连续可微函数,$x \in R^n$.考虑以下三类优化问题. 1.无约束的优化问题: \begin{align* ...
- 静态资源压缩(GZIP) 专题
1.开GZIP有什么好处?答:Gzip开启以后会将输出到用户浏览器的数据进行压缩的处理,这样就会减小通过网络传输的数据量,提高浏览的速度.Tips:如果网站的用户分布比较分散,并且静态文件过大,可以将 ...
- 对于bootstrap可视化布局设计可以参考2017.6.2
http://www.layoutit.cn/v3/index.html 可视化布局链接,拖好控件自动生成代码非常方便 http://www.jq22.com/jquery-info14044 这 ...
- Android开发环境的配置2017.05.27
关于配置Android开发环境请参考此链接:http://blog.chinaunix.net/uid-25434387-id-461933.html
- Python使用Socket写一个简单聊天程序
b2b模式的聊天工具 服务端: # 链接 while True: print('等待连接...') sock,adr = server_socket.accept() while True: try: ...
- Python-分支循环- if elif for while
分支与循环 条件是分支与循环中最为核心的点,解决的问题场景是不同的问题有不同的处理逻辑.当满足单个或者多个条件或者不满足条件进入分支和循环,这里也就说明这个对相同问题处理执行逻辑依据具体参数动态变化, ...
- synchronized和volatile简介
简介 volatile是一个变量修饰符,而synchronized是一个方法或块的修饰符.所以我们使用这两种关键字来指定三种简单的存取变量的方式. 2345678 int i1;int geti1() ...
- noip 2015 斗地主 大爆搜!!!
反正肯定是大模拟 但是每一个可以出的牌都搜一定不是最优的 考虑最特殊的出牌方案:顺子(单,对,三) 每一种方案再加上暴力贪心打出剩下的牌的步数 #include<cstdio> #incl ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- BZOJ_2693_jzptab_莫比乌斯反演
BZOJ_2693_jzptab_莫比乌斯反演 Description Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 表示N.M Output T行 每行一个整数 表示第i组数据的 ...