如何扩展实现自定义的属性编辑器
- 自定义一个实现了PropertyEditorSupport接口的编辑器
- 让spring能够识别到此编辑器,自定义实现一个属性编辑器的注册器,实现了PropertyEditorRegistrar接口
- 让spring能够识别到对应的注册器
导航,文件结构
程序入口
package com.mashibing;
import com.mashibing.selfEditor.Customer;
public class TestCustomer {
//01 源码分析-spring的bean工厂准备工作.vep
//prepareBeanfactory
public static void main(String[] args) {
MyClassPathXmlApplicationContext ac = new MyClassPathXmlApplicationContext("selfEditor.xml");
Customer bean = ac.getBean(Customer.class);
System.out.println(bean);
}
}
Address类解析器注册
package com.mashibing.selfEditor;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Address.class,new AddressPropertyEditor());
}
}
Address类解析器,将address值转换为Address类
package com.mashibing.selfEditor;
import java.beans.PropertyEditorSupport;
public class AddressPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] s = text.split("_");
Address address = new Address();
address.setProvince(s[0]);
address.setCity(s[1]);
address.setTown(s[2]);
this.setValue(address);
}
}
selfEditor.xml 有两种方式注入值
测试用例
https://download.csdn.net/download/weixin_50750933/85749576
相关类
CustomEditorConfigurer.java
注意查看此文件最后一个方法postProcessBeanFactory,可以选择两种方式进行注入。
package org.springframework.beans.factory.config;
import java.beans.PropertyEditor;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
public class CustomEditorConfigurer implements BeanFactoryPostProcessor, Ordered {
protected final Log logger = LogFactory.getLog(getClass());
private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
@Nullable
private PropertyEditorRegistrar[] propertyEditorRegistrars;
@Nullable
private Map, Class extends PropertyEditor>> customEditors;
public void setOrder(int order) {
this.order = order;
}
@Override
public int getOrder() {
return this.order;
}
public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) {
this.propertyEditorRegistrars = propertyEditorRegistrars;
}
public void setCustomEditors(Map, Class extends PropertyEditor>> customEditors) {
this.customEditors = customEditors;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// 如果属性编辑注册器不等于空
if (this.propertyEditorRegistrars != null) {
// 遍历属性编辑注册器的集合
for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {
// 将属性编辑注册器添加到beanFactory
beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
}
}
// 如果自定义编辑器不等于空
if (this.customEditors != null) {
// 遍历自定义编辑器集合将自定义编辑器添加到beanFactory中
this.customEditors.forEach(beanFactory::registerCustomEditor);
}
}
}
疑问:editor通过PropertyEditorRegistrySupport.customEditors属性获得自定义解析器。
如果我注入的是propertyEditorRegistrar,则实在那里转换为customEditors的。
package org.springframework.beans;
TypeConverterDelegate.convertIfNecessaryconvertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue,
@Nullable Class requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException {}
PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);



