Spring 4 Custom Editor Configurer Example in java

View: 1351    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Javascript   Fields: Other

9 point/1 review File has been tested

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-inCustomDateEditor class into your application.

Introduction

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-inCustomDateEditor 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.

Spring 4 Custom Editor Configurer Example in java

Spring 4 Custom Editor Configurer Example in java Posted on 30-07-2016  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-inCustomDateEditor class into your application. 9/10 1351

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 608    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    1.5/3 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 711    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    1.5/3 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 322    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 340    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 348    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    4.5/2 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 330    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 5102    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 789    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    4.5/1 review
    Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd