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做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
随机推荐
- JSONUtils.toJSONString的一个坑
JSONUtils.toJSONString(null); //返回一个为"null"的字符串 这样会导致一个结果就是StringUtils.isBlank判断后,会为false ...
- jQuery学习之旅 Item7 区别this和$(this)
刚开始以为this和$(this)就是一模子刻出来.但是我在阅读时,和coding时发现,总不是一回事,这里就谈谈this与$(this)的区别. 1.jQuery中this与$(this)的区别 $ ...
- 如何解决testng执行用例失败自动重跑问题
注: 以下内容引自 http://blog.csdn.net/MenofGod/article/details/72846649 看过几个相关问题的帖子,内容类似,不过这篇解决问题的步骤和代码比较清晰 ...
- 问题(一) DebugAugmenter
问题: DebugAugmenter的作用是什么?是任何一个自创建的变量都可以取代它还是它有特定含义? public class DebugAugmenter Test { @Test public ...
- java没有firendly访问类型
java中只有public.private.protected.default这几种修饰符,没有friendly修饰符,没加修饰符就是friendly.friendly只是一种说法,把它认为是defa ...
- index_levedb.go
) binary.BigEndian.PutUint64(key, fid) return l.db.Delete(key, nil) } //关闭资源 func (l *LevelD ...
- 深入理解springAOP切面的特性
一张图说明情况
- Apache SkyWalking 为.NET Core带来开箱即用的分布式追踪和应用性能监控
在大型网站系统设计中,随着分布式架构,特别是微服务架构的流行,我们将系统解耦成更小的单元,通过不断的添加新的.小的模块或者重用已经有的模块来构建复杂的系统.随着模块的不断增多,一次请求可能会涉及到十几 ...
- NavigationView头部设置监听事件
直接写解决方法吧: 1.将XML里的静态引入删除: <android.support.design.widget.NavigationView android:id="@+id/nav ...
- 【爆料】-《阿伯丁大学毕业证书》AU一模一样原件
☞阿伯丁大学毕业证书[微/Q:865121257◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&a ...