Spring 4 CustomEditorConfigurer Example--转

原文地址:http://howtodoinjava.com/spring/spring-core/registering-built-in-property-editors-in-spring-4-customeditorconfigurer-example/

A property editor is a feature of the JavaBeans API for converting property values to and from text values. Each property editor is designed for a certain type of property only. You may wish to employ property editors to simplify your bean configurations. In this tutorial, we will learn to configure spring’s build-in CustomDateEditor class into your application.

CustomEditorConfigurer and CustomDateEditor configurations

Normally, you will register a property editor in the container before it may be used. The CustomEditorConfigurer class is implemented as a built-in bean factory post processor for you to register your custom property editors before any of the beans get instantiated.

For example, in your application if you want to convert date values from string format to java.util.Date objects or vice-versa, you can use CustomDateEditor class. The CustomDateEditor class that comes with Spring is for converting date strings into java.util.Date properties.

CustomEditorConfigurer bean can be declared into application context as below:

< bean class = "org.springframework.beans.factory.config.CustomEditorConfigurer" >
     < property name = "propertyEditorRegistrars" >
         < list >
             < bean class = "com.howtodoinjava.demo.processors.CustomDateEditorRegistrar" />
         </ list >
     </ property >
</ bean >

CustomDateEditorRegistrar class should be declared in below manner from spring 4.x onwards.

public class CustomDateEditorRegistrar implements PropertyEditorRegistrar
{
     public void registerCustomEditors(PropertyEditorRegistry registry)
     {
         registry.registerCustomEditor(Date. class ,
                 new CustomDateEditor( new SimpleDateFormat( "yyyy-MM-dd" ), false ));
     }
}

CustomDateEditor Example

Now everytime, when you pass a bean property value (of type java.util.Date) in string format e.g. 2007-09-30, it will be automatically converted to date object.

Let’s Test the configuration. To test, I have created a EmployeeDTO bean having one date field as dateOfBirth.

public class EmployeeDTO {
     
     private Integer id;
     private String firstName;
     private String lastName;
     private String designation;
     private Date dateOfBirth;
 
     //Setters and Getters
 
     @Override
     public String toString() {
         return "EmployeeDTO [id=" + id + ", firstName=" + firstName
                 + ", lastName=" + lastName + ", designation=" + designation
                 + ", dateOfBirth=" + dateOfBirth + "]" ;
     }
}

It’s bean definition in applicationContext.xml file is as below:

< bean class = "org.springframework.beans.factory.config.CustomEditorConfigurer" >
     < property name = "propertyEditorRegistrars" >
         < list >
             < bean class = "com.howtodoinjava.demo.processors.CustomDateEditorRegistrar" />
         </ list >
     </ property >
</ bean >
 
<!-- employeeDTO bean -->
< bean id = "employeeDTO" class = "com.howtodoinjava.demo.model.EmployeeDTO" >
     < property name = "firstName" value = "Lokesh" />
     < property name = "lastName" value = "Gupta" />
     < property name = "designation" value = "Manager" />
     < property name = "dateOfBirth" value = "2007-09-30" />
</ bean >

Let’s fetch the bean from context. It should have it’s dateOfBirth filed populated with given date value.

public class TestSpringContext
{
     @SuppressWarnings ( "resource" )
     public static void main(String[] args) throws Exception
     {
         ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );
 
         EmployeeDTO employeeDTO = (EmployeeDTO) context.getBean( "employeeDTO" );
         
         System.out.println(employeeDTO.getDateOfBirth());
     }
}
 
Output:
 
Sun Sep 30 00 : 00 : 00 IST 2007

Great. Date value is set.

Happy Learning !!

转载于:https://www.cnblogs.com/davidwang456/p/6688926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值