Spring的大部分默认属性编辑器都直接扩展于java.beans.PropertyEditorSupport类,开发者也可以通过扩展PropertyEditorSupport实现自己的属性编辑器。
(在IoC的流水线的第三步中,Bean工厂后处理器BeanFactoryPostProcessor对BeanDefinitionRegistry中的BeanDefinition进行扫描,通过Java反射机制找出所有属性编辑器的Bean(实现java.beans.PropertyEditor接口的Bean),并自动将它们注册到Spring容器的PropertyEditorRegistry中。)
public class CustomEditorConfigurer implements BeanFactoryPostProcessor, Ordered {
protected final Log logger = LogFactory.getLog(this.getClass());
private int order = 2147483647;
private PropertyEditorRegistrar[] propertyEditorRegistrars;
//将customEditors中的键值对注册到Spring容器的PropertyEditorRegistry中
private Map<Class<?>, Class<? extends PropertyEditor>> customEditors;
public CustomEditorConfigurer() {
}
public void setOrder(int order) {
this.order = order;
}
public int getOrder() {
return this.order;
}
public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) {
this.propertyEditorRegistrars = propertyEditorRegistrars;
}
public void setCustomEditors(Map<Class<?>, Class<? extends PropertyEditor>> customEditors) {
this.customEditors = customEditors;
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (this.propertyEditorRegistrars != null) {
PropertyEditorRegistrar[] var2 = this.propertyEditorRegistrars;
int var3 = var2.length;
for(int var4 = 0; var4 < var3; ++var4) {
PropertyEditorRegistrar propertyEditorRegistrar = var2[var4];
beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
}
}
if (this.customEditors != null) {
Iterator var6 = this.customEditors.entrySet().iterator();
while(var6.hasNext()) {
Entry<Class<?>, Class<? extends PropertyEditor>> entry = (Entry)var6.next();
Class<?> requiredType = (Class)entry.getKey();
Class<? extends PropertyEditor> propertyEditorClass = (Class)entry.getValue();
//将它们注册到Spring容器的PropertyEditorRegistry中
beanFactory.registerCustomEditor(requiredType, propertyEditorClass);
}
}
}
}
一个例子:
Boss类:
public class Boss {
private String name;
private Car car = new Car();
public String toString(){
String temp ="name:"+name+"\n";
temp += "car:"+car;
return temp;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Car类:
public class Car {
private int maxSpeed;
public String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return "brand:"+brand+"/maxSpeed:"+maxSpeed+"/price:"+price;
}
}
PropertyEditor:
public class CustomCarEditor extends PropertyEditorSupport {
//覆盖setAsText(),自定义属性编辑方式!!!
public void setAsText(String text){
if(text == null || text.indexOf(",") == -1){
throw new IllegalArgumentException("设置的字符串格式不正确");
}
String[] infos = text.split(",");
Car car = new Car();
car.setBrand(infos[0]);
car.setMaxSpeed(Integer.parseInt(infos[1]));
car.setPrice(Double.parseDouble(infos[2]));
setValue(car);
}
public String getAsText() {
Object value = getValue();
if(value == null){
return "";
}else{
Car car = (Car)value;
return car.getBrand()+","+car.getMaxSpeed()+","+car.getPrice();
}
}
}
通过配置文件将CustomCarEditor添加到BeanFacotryPostProcessor的实现类CustomEditorConfigurar中的属性customEditors中:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.smart.editor.Car"
value="com.smart.editor.CustomCarEditor"/>
</map>
</property>
</bean>
<bean id="boss" class="com.smart.editor.Boss">
<property name="name" value="John"/>
//自定义属性编辑器!!!
<property name="car" value="红旗CA72,200,20000.00"/>
</bean>