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 XmlNewEditorExample {

public static void main (String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("spring-config3.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");
}

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}}

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.XmlConfigExample.Order">
<property name="amount" value="102"/>
<property name="currency" value="USD"/>
</bean>
</beans>
Original Post




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.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XmlNewEditorExample2 {

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

public static class MyCustomEditorRegistrar implements PropertyEditorRegistrar {

@Override
public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(
XmlNewEditorExample.Phone.class, "phone", new XmlNewEditorExample.CustomPhoneEditor());
}
}
}

Output

Customer{customerName='Steve Thomas', phone=Phone{phoneNumber='907-111-2123', phoneType=CELL}}

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.XmlConfigExample.Order">
<property name="amount" value="102"/>
<property name="currency" value="USD"/>
</bean>
</beans>
Original Post




See Also