Custom PropertyEditors Spring Example

By Arvind Rai, July 13, 2022
In Spring, when we inject value as string, internally Spring uses built-in PropertyEditors to change that string to actual object. We can also create custom PropertyEditor that will create required object using injected values.
Spring provides PropertyEditorSupport to create custom PropertyEditor. We need to extend PropertyEditorSupport and need to override setAsText method to create required object from string.
Let us understand step-by-step.

Example

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;
	}
} 
Find the bean configuration.
<bean id="person" class="com.concretepage.bean.Person">
    <property name="name" value="Ram" />
    <property name="type" value="admin" />
</bean> 
Now find the custom PropertyEditors.
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()));
    }
} 

Now we need to register our PersonTypeEditor to application context. 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 : The CustomEditorConfigurer has a collection property customEditors that accepts custum PropertyEditors.

Now run the main method to check the output.
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());
	}
} 
What we have done is that PersonTypeEditor will return PersonType object with upper case property value.
Output
ADMIN 

Reference

Class PropertyEditorSupport

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us