Tuesday, July 31, 2012

Spring Custom Property Editor


Custom Property Editor is great concept in spring using this we convert strings into beans. For this, we need to write editor by extending PropertyEditorSupport and this will be processed by CustomEditorConfigurer and creates the bean.
Contact:
package com.lnn.custom.property.editors;

public class Contact {
      private PhoneNumber phoneNumber;

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

      public PhoneNumber getPhoneNumber() {
            return phoneNumber;
      }
}
PhoneNumber:
package com.lnn.custom.property.editors;

public class PhoneNumber {
      private String areaCode = null;
      private String prefix = null;
      private String number = null;

      public PhoneNumber(String areaCode, String prefix, String number) {
            this.areaCode = areaCode;
            this.prefix = prefix;
            this.number = number;
      }

      @Override
      public String toString() {
            return "PhoneNumber [areaCode=" + areaCode + ", prefix=" + prefix + ", number=" + number + "]";
      }
}
PhoneEditor:
package com.lnn.custom.property.editors;

import java.beans.PropertyEditorSupport;

public class PhoneEditor extends PropertyEditorSupport {
      public void setAsText(String textValue) {
            String stripped = stripNonNumeric(textValue);
            String areaCode = stripped.substring(0, 3);
            String prefix = stripped.substring(3, 6);
            String number = stripped.substring(6);
            PhoneNumber phone = new PhoneNumber(areaCode, prefix, number);
            setValue(phone);
      }

      private String stripNonNumeric(String original) {
            StringBuffer allNumeric = new StringBuffer();
            for (int i = 0; i < original.length(); i++) {
                  char c = original.charAt(i);
                  if (Character.isDigit(c)) {
                        allNumeric.append(c);
                  }
            }
            return allNumeric.toString();
      }
}
editors.xml:
<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-3.0.xsd">
      <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
            <property name="customEditors">
                  <map>
                        <entry key="com.lnn.custom.property.editors.PhoneNumber">
                              <bean id="phoneEditor" class="com.lnn.custom.property.editors.PhoneEditor" />
                        </entry>
                  </map>
            </property>
      </bean>
      <bean id="contact" class="com.lnn.custom.property.editors.Contact">
            <property name="phoneNumber" value="123-456-7890" />
      </bean>
</beans>
Test:
package com.lnn.custom.property.editors;

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

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("editors.xml");
            Contact contact = context.getBean("contact", Contact.class);
            System.out.println(contact.getPhoneNumber());
      }
}

No comments:

Post a Comment