Home Tutorial Spring Spring3 Ioc Spring CustomEditorConfigurer, Spring Custom Editor

 
 

Share on Google+Share on Google+
Spring CustomEditorConfigurer, Spring Custom Editor
Posted on: September 1, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn about Spring CustomEditorConfigurer.

Spring CustomEditorConfigurer

Passing a date format in the bean property is not allowed and in order to do so you need to register the CustomDateEditor in CustomEditorConfigurer first so that it will convert the bean properties which type is java.util.Date

Employee.java

package spring.date.property;

import java.util.Date;

public class Employee {

	private Date joiningDate;
	private String name;

	public Date getJoiningDate() {
		return joiningDate;
	}

	public void setJoiningDate(Date joiningDate) {
		this.joiningDate = joiningDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

AppMain.java

package spring.date.property;

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

public class AppMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		Employee emp = (Employee) context.getBean("bean1");
		System.out.println(emp.getJoiningDate());
	}
}

context.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-2.5.xsd">

  <bean id="dateEditor"
    class="org.springframework.beans.propertyeditors.CustomDateEditor">

    <constructor-arg>
      <bean class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />
      </bean>
    </constructor-arg>
    <constructor-arg value="true" />

  </bean>
  <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
      <map>
        <entry key="java.util.Date">
          <ref local="dateEditor" />
        </entry>
      </map>
    </property>
  </bean>

  <bean id="bean1" class="spring.date.property.Employee">
    <property name="name" value="Rakesh" />
    <property name="joiningDate" value="2010-01-30" />
  </bean>

</beans>

When you run this application it will display message as shown below:


Sat Jan 30 00:00:00 GMT+05:30 2010

Download this example code

Advertisement

Related Tags for Spring CustomEditorConfigurer, Spring Custom Editor:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 1, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com