Home  >  Spring Core

Custom PropertyEditors Spring Example

By Arvind Rai, August 19, 2013
In spring, when we inject setter value as string, internally spring uses build in PropertyEditors to change that string to actual object. According to the need, we can create custom PropertyEditors that will create required object by given injected value. Letís understand step by step. Find the beans Person and PersonType.

Person.java
package com.concretepage.bean;
public class Person {
	private String name;
	private PersonType type;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public PersonType getType() {
		return type;
	}
	public void setType(PersonType type) {
		this.type = type;
	}
}
 


PersonType.java
package com.concretepage.bean;
public class PersonType {
	private String typeName;
	public PersonType(String typeName){
		this.typeName = typeName; 
	}
	public PersonType(){ 
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
}
 
Person class has a property of PersonType. Now find the XMl code which is injecting value to Person. For the vaiable type, we need PersonType Object, but we have injected String i.e ďadminĒ.
<bean id="person" class="com.concretepage.bean.Person">
    <property name="name" value="Ram" />
    <property name="type" value="admin" />
</bean>
 
Now Here is the of custom PropertyEditors. Find the custom property editors below.

PersonTypeEditor.java
package com.concretepage;
import java.beans.PropertyEditorSupport;
import com.concretepage.bean.PersonType;
public class PersonTypeEditor extends PropertyEditorSupport  {
	public void setAsText(String text) {
        setValue(new PersonType(text.toUpperCase()));
    }
}
 

PropertyEditorSupport in Spring

To create custom PropertyEditors, we need to extend PropertyEditorSupport and need to override setAsText method to create required object from string. Now we need to register this PersonTypeEditor in spring to work. Find the XML code.

spring.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="person" class="com.concretepage.bean.Person">
      <property name="name" value="Ram" />
      <property name="type" value="admin" />
    </bean>
    
    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
      <property name="customEditors">
       <map>
         <entry key="com.concretepage.bean.PersonType" value="com.concretepage.PersonTypeEditor"/>
       </map>
      </property>
    </bean>
       
</beans>
 

CustomEditorConfigurer in Spring

CustomEditorConfigurer has a collection property customEditors that takes custum PropertyEditors. Now run the main method to check the output. What we have done is that PersonTypeEditor will return PersonType object with Upper case property value.

SpringTest.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Person;
import com.concretepage.bean.PersonType;
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext  context = new ClassPathXmlApplicationContext("spring.xml");
		Person person = (Person)context.getBean("person");
		PersonType ptype = person.getType();
		System.out.println(ptype.getTypeName());
	}
}   
 


Output
ADMIN
 
Download Source Code
custom-propertyeditors-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us