Saturday, October 29, 2022

Spring - Property Editors

  • The main intention of the PropertyEditors is to convert data from text to Object and from Object to text.
  • In general, in J2SE, in Java Beans, PropertyEditor was originally designed to be used in Swing applications. JavaBeans 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 PropertyEditors 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,....., In this context, to convert data from textual rep-resentation 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.

  • Create User defined Property Editor class by extending java.beans.PropertyEditorSupport class.
  • Override setAsText(---) method in user defined Property Editor.
  • 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.
  • Prepare Spring application as it is.

Example: 

EmployeeAddress.java

package com.cloud.beans; 
public class EmployeeAddress {
	private String pno; 
	private String street; 
	private String city; 
	private String country;

	public String getPno() {
		return pno; 
	}

	public void setPno(String pno) {
		this.pno = pno; 
	}

	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 getCountry() {
		return country; 
	}

	public void setCountry(String country) {
		this.country = country; 
	}
}

Employee.java

package com.cloud.beans; public class Employee {

	private String eid;
	private String ename;
	private float esal;
	private EmployeeAddress eaddr;

	public String getEid() {
		return eid; 
	}

	public void setEid(String eid) {
		this.eid = eid; 
	}

	public String getEname() {
		return ename; 
	}

	public void setEname(String ename) {
		this.ename = ename; 
	}

	public float getEsal() {
		return esal; 
	}

	public void setEsal(float esal) {
		this.esal = esal; 
	}

	public EmployeeAddress getEaddr() {
		return eaddr; 
	}

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

	public void getEmpDetails(){ 
		System.out.println("Employee Details");
		System.out.println("------------------------"); 
		System.out.println("Employee Id :"+eid); 
		System.out.println("Employee Name :"+ename); 
		System.out.println("Employee Salary :"+esal); 

		System.out.println("Employee Address Details:"); 
		System.out.println("-----------------------------"); 
		System.out.println("PNO :"+eaddr.getPno()); 
		System.out.println("STREET :"+eaddr.getStreet()); 
		System.out.println("CITY :"+eaddr.getCity()); 
		System.out.println("COUNTRY :"+eaddr.getCountry());
	}
}

EmployeeAddressEditor.java

package com.cloud.beans;
import java.beans.PropertyEditorSupport;

public class EmployeeAddressEditor extends PropertyEditorSupport{
	
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		String[] str = text.split("-"); System.out.println(text);
		EmployeeAddress eaddr = new EmployeeAddress(); eaddr.setPno(str[0]);
		eaddr.setStreet(str[1]);
		eaddr.setCity(str[2]);
		eaddr.setCountry(str[3]);
		super.setValue(eaddr);
	} 
}

Test.java

package com.cloud.test;
import com.cloud.beans.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args)throws Exception {
		ApplicationContext context = 
			new ClassPathXmlApplicationContext("/com/cloud/cfgs/spring_beans_config.xml");
		Employee emp = (Employee) context.getBean("emp");
		emp.getEmpDetails();
	 }

}

spring_beans_config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans>
	<bean id="emp" class="com.cloud.beans.Employee"> 
		<property name="eid" value="E-111"/>
		<property name="ename" value="cloud"/>
		<property name="esal" value="50000"/>
		<property name="eaddr" value="23/3rt-M G Road-Hyd-India"/>
	</bean>

	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
		<map>
			<entry key="com.cloud.beans.EmployeeAddress" 
					value="com.cloud.beans.EmployeeAddressEditor"/>
		</map> 
		</property>

	</bean>
</beans>

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS