Recent Posts

Saturday, 22 June 2019

Property Editors

Property Editors
     The main intention of the Property Editors is to convert data from text to Object and from Object to text. In general, in J2SE, in Java Beans, Property Editor was originally designed to be used in Swing applications. Java Beans specification defines API to introspect and extract the bean inner details which can be used to show bean properties visually as components and edit them by using Property Editors in Build Tools.
In Spring Applications, we will provide all values in spring configuration file as text values , but, Spring framework has to store these text values into the bean objects as the objects like Byte, Integer, String, Long, etc. In this context, to convert data from textual representation to the respective objects Spring framework will use a feature "Property Editors". To convert data from text form to Objects, Spring Framework has provided the following Predefined Property Editors.
1. ByteArrayPropertyEditor: Editor for byte arrays. Strings will simply be converted to their corresponding byte representations.
2. ClassEditor: Parses Strings representing classes to actual classes.
3. CustomBooleanEditor: Customizable property editor for Boolean properties.
4. CustomCollectionEditor: Property editor for Collections, converting any source Collection to a given target Collection type. Custom Date Editor Customizable property editor for java.util.Date, supporting a custom Date Format.
5. CustomNumberEditor: Customizable property editor for any Number subclass like Integer, Long, Float, Double.
6. FileEditor: Capable of resolving Strings to java.io.File objects.
7. InputStreamEditor: One-way property editor, capable of taking a text string and producing (via an intermediate ResourceEditor and Resource) an InputStream, so InputStream properties may be directly set as Strings.
8. LocaleEditor: Capable of resolving Strings to Locale objects and vice versa (the String format is [country][variant], which is the same thing the toString() method of Locale provides).
9. PatternEditor: Capable of resolving Strings to java.util.regex.Pattern objects and vice versa.
10. PropertiesEditor: Capable of converting Strings (formatted using the format as defined in the javadocs of the java.util.Properties class) to Properties objects.
11. StringTrimmerEditor: Property editor that trims Strings. Optionally allows transforming an empty string into a null value.
12. URLEditor: Capable of resolving a String representation of a URL to an actual URL object.
     Spring Framework has provided an approach to provide custom Property Editors, for this , we have to use the following steps.
1. Create User defined Property Editor class by extending java.beans.PropertyEditorSupport class.
2. Override setAsText(---) method in user defined Property Editor.
3. Configure org.springframework.beans.factory.config.CustomEditorConfigurer in spring configuration file with the property "customEditors" of Map type with a key-value pair, where key is the class type for which the property editor is defined and value is the custom property editor.
4. Prepare Spring application as it is.
package com.ashok.spring.core.bean.propertyeditor.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Address {
   private String street;
   private String city;
   private String state;

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }

   @Override
   public String toString() {
      return "Address [street=" + street + ", city=" + city + ", state=" + state + "]";
   }
}
package com.ashok.spring.core.bean.propertyeditor.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private Address eaddr;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public Address getEaddr() {
      return eaddr;
   }

   public void setEaddr(Address eaddr) {
      this.eaddr = eaddr;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", eaddr=" + eaddr +"]";
   }
}
package com.ashok.spring.core.bean.propertyeditor.beans;

import java.beans.PropertyEditorSupport;

public class AddressEditor extends PropertyEditorSupport {
   @Override
   public void setAsText(String text) {
      String[] str = text.split("-");
      System.out.println(text);
      Address eaddr = new Address();
      eaddr.setStreet(str[1]);
      eaddr.setCity(str[2]);
      eaddr.setState(str[3]);
      super.setValue(eaddr);
   }
}
<?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-3.0.xsd">

   <bean id="emp" class="com.ashok.spring.core.bean.propertyeditor.beans.Employee">
      <property name="empId" value="E0087" />
      <property name="empName" value="Ashok Kumar" />
      <property name="eaddr" value="23/3rt-Tanuku Road-Bhimavaram-Andhrapradesh" />
   </bean>
   <bean
      class="org.springframework.beans.factory.config.CustomEditorConfigurer">
      <property name="customEditors">
         <map>
            <entry key="com.ashok.spring.core.bean.propertyeditor.beans.Address"
               value="com.ashok.spring.core.bean.propertyeditor.beans.AddressEditor" />
         </map>
      </property>
   </bean>
</beans>
package com.ashok.spring.core.bean.propertyeditor.editor;

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

import com.ashok.spring.core.bean.propertyeditor.beans.Employee;

public class TestPropertyEditor {
   @SuppressWarnings("resource")
   public static void main(String[] args) throws Exception {
       String path = "/com/ashok/spring/core/bean/propertyeditor/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(path);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}
Output
23/3rt-Tanuku Road-Bhimavaram-Andhrapradesh
Employee [empName=Ashok Kumar, empId=E0087, eaddr=Address [street=Tanuku Road, city=Bhimavaram, state=Andhrapradesh]]

Next Tutorial : Profiling in Spring

Previous Tutorial : Bean Manipulations and Bean Wrappers


No comments:

Post a Comment