Spring placeholder默认值设置
2018-01-19 15:36
197 查看
看到canal源码的时候发现了好多这样的写法
/**
* 扩展Spring的
* {@linkplain org.springframework.beans.factory.config.PropertyPlaceholderConfigurer}
* ,增加默认值的功能。 例如:${placeholder:defaultValue},假如placeholder的值不存在,则默认取得
* defaultValue。
* @author jianghang 2013-1-24 下午03:37:56
* @version 1.0.0
* */
看一下类图
![](//upload-images.jianshu.io/upload_images/2847417-3f8065c6f9efef07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
PropertyPlaceholderConfigurer类图这里只要实现了InitializingBean的afterPropertiesSet方法 和 ResourceLoaderAware 的setResourceLoader方法将资源的路径解析出来,然后加载资源 。重写了 PropertyPlaceholderConfigurer 的resolvePlaceholder 方法,先解析成DefaultablePlaceholder,然后发现取不到一个属性的时候将默认值赋给该属性,其中默认值是冒号后面的值。DefaultablePlaceholder 是新定义的内部类。
贴出主要实现的代码
原文链接:https://www.jianshu.com/p/6f5a9a02b2d2
<bean id="eventParser" class="com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser"> <property name="destination" value="${canal.instance.destination}" /> <property name="slaveId" value="${canal.instance.mysql.slaveId:1234}" /> <!-- 心跳配置 --> <property name="detectingEnable" value="${canal.instance.detecting.enable:false}" /> <property name="detectingSQL" value="${canal.instance.detecting.sql}" /> <property name="detectingIntervalInSeconds" value="${canal.instance.detecting.interval.time:5}" /> <property name="haController"> <bean class="com.alibaba.otter.canal.parse.ha.HeartBeatHAController"> <property name="detectingRetryTimes" value="${canal.instance.detecting.retry.threshold:3}" /> <property name="switchEnable" value="${canal.instance.detecting.heartbeatHaEnable:false}" /> </bean> </property>property的属性${canal.instance.mysql.slaveId:1234} 取配置文件key的时候带了:后面跟了一个值 后来才发现这是对spring placeholder 设置默认值的一种扩展。
/**
* 扩展Spring的
* {@linkplain org.springframework.beans.factory.config.PropertyPlaceholderConfigurer}
* ,增加默认值的功能。 例如:${placeholder:defaultValue},假如placeholder的值不存在,则默认取得
* defaultValue。
* @author jianghang 2013-1-24 下午03:37:56
* @version 1.0.0
* */
看一下类图
![](http://upload-images.jianshu.io/upload_images/2847417-3f8065c6f9efef07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
PropertyPlaceholderConfigurer类图这里只要实现了InitializingBean的afterPropertiesSet方法 和 ResourceLoaderAware 的setResourceLoader方法将资源的路径解析出来,然后加载资源 。重写了 PropertyPlaceholderConfigurer 的resolvePlaceholder 方法,先解析成DefaultablePlaceholder,然后发现取不到一个属性的时候将默认值赋给该属性,其中默认值是冒号后面的值。DefaultablePlaceholder 是新定义的内部类。
贴出主要实现的代码
@Override protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) { DefaultablePlaceholder dp = new DefaultablePlaceholder(placeholder); String value = super.resolvePlaceholder(dp.placeholder, props, systemPropertiesMode); if (value == null) { value = dp.defaultValue; } return trimToEmpty(value); } private static class DefaultablePlaceholder { private final String defaultValue; private final String placeholder; public DefaultablePlaceholder(String placeholder){ int commaIndex = placeholder.indexOf(":"); String defaultValue = null; if (commaIndex >= 0) { defaultValue = trimToEmpty(placeholder.substring(commaIndex + 1)); placeholder = trimToEmpty(placeholder.substring(0, commaIndex)); } this.placeholder = placeholder; this.defaultValue = defaultValue; } }
原文链接:https://www.jianshu.com/p/6f5a9a02b2d2
相关文章推荐
- 给Spring的placeholder设置默认值
- 动态设置spring配置PropertyPlaceholderConfigurer location的路径
- spring @Value 设置默认值
- 【第十五章】 springboot + pojo默认值设置
- spring 配置文件属性设置默认值以及读取环境变量值
- 关于 Spring 中使用 context:property-placeholder 属性占位符设置配置文件
- 动态设置spring配置PropertyPlaceholderConfigurer location的路径
- 第十五章 springboot + pojo默认值设置
- [Spring]设置property-placeholder绝对路径
- spring @Value 设置默认值
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
- 在 Spring.NET 中使用 PropertyPlaceholderConfigurer 设置配置参数
- spring @Value 设置默认值
- 第十五章 springboot + pojo默认值设置
- Spring @Value 设置默认值
- Quartz(一)在Spring中设置动态定时任务
- 使用angularjs的ng-options时如何设置默认值(初始值)
- 打开编辑界面给表格设置默认值
- MyBatis与Spring设置callSettersOnNulls
- 为多表批量添加字段,设置默认值,赋值SQL(自编)