Close

Spring Framework - CustomEditorConfigurer Examples

Spring Framework 

This example shows how to create a custom editor and register it for the target type 'Phone' using CustomEditorConfigurer.

package com.logicbig.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.PropertyEditorSupport;

public class XmlUserCustomEditorExample {

public static void main (String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("spring-config.xml");
Customer bean = context.getBean(Customer.class);
System.out.println(bean);
}

public static class Customer {
private String customerName;
private Phone phone;

public String getCustomerName () {
return customerName;
}

public void setCustomerName (String customerName) {
this.customerName = customerName;
}

public Phone getPhone () {
return phone;
}

public void setPhone (Phone phone) {
this.phone = phone;
}

@Override
public String toString () {
return "Customer{" +
"customerName='" + customerName + '\'' +
", phone=" + phone +
'}';
}
}

public static class Phone {
private String phoneNumber;
private PhoneType phoneType;

public String getPhoneNumber () {
return phoneNumber;
}

public void setPhoneNumber (String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public PhoneType getPhoneType () {
return phoneType;
}

public void setPhoneType (PhoneType phoneType) {
this.phoneType = phoneType;
}

@Override
public String toString () {
return "Phone{" +
"phoneNumber='" + phoneNumber + '\'' +
", phoneType=" + phoneType +
'}';
}
}

public static enum PhoneType {
LAND,
CELL,
WORK
}

public static class CustomPhoneEditor extends PropertyEditorSupport {

@Override
public void setAsText (String text) throws IllegalArgumentException {
String[] split = text.split("[|]");
if (split.length != 2) {
throw new IllegalArgumentException(
"Phone is not correctly defined. The correct format is " +
"PhoneType|111-111-1111");
}
//the method is already throwing IllegalArgumentException
//so don't worry about split[0] invalid enum value.
PhoneType phoneType = PhoneType.valueOf(split[0].trim()
.toUpperCase());
Phone phone = new Phone();
phone.setPhoneType(phoneType);
phone.setPhoneNumber(split[1].trim());
setValue(phone);

}
}
}

Output

Customer{customerName='Steve Thomas', phone=Phone{phoneNumber='907-111-2123', phoneType=CELL}}
INFO: Loading XML bean definitions from class path resource [spring-config.xml]

src/main/resources/spring-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="customer" class="com.logicbig.example.XmlUserCustomEditorExample.Customer">
<property name="customerName" value="Steve Thomas"/>
<property name="phone" value="cell | 907-111-2123"/>
</bean>

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.logicbig.example.XmlUserCustomEditorExample$Phone"
value="com.logicbig.example.XmlUserCustomEditorExample$CustomPhoneEditor"/>
</map>
</property>
</bean>
</beans>
Original Post




See Also