环境:
spring 4.1.2
一共四步:
第一步:
资源文件添加key-value:
memcached.opTimeout=10000
第二步:
实现类型处理的editor
public class LongPropertyEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
this.setValue(Long.valueOf(text));
}
}
第三步:
配置自定义的long类型处理editor
<!-- 特殊属性的注入.把特殊属性注入到CustomEditorConfigurer Bean 里 -->
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="long" value = "com.joyworks.memcached.editor.LongPropertyEditor"/>
</map>
</property>
</bean>
第四步:
使用配置
<property name="opTimeout" value="${memcached.opTimeout}"></property>
断点方式测试:
1)TypeConverterDelegate.convertIfNecessary(String, Object, Object, Class<T>, TypeDescriptor)的
PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);
加上断点,这句代码的意思就是根据配置文件value的类型来寻找咱们自定义的editor,然后使用自定义的editor来进行转换
2)如果想要测试有没有成功,直接在LongPropertyEditor的setAsText方法上设置断点,看代码有没有经过自定义方法中