原文地址: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 !!

Spring 4 CustomEditorConfigurer Example--转相关推荐

  1. Spring自定义属性编辑器PropertyEditorSupport + 使用CustomEditorConfigurer注册属性编辑器...

    1.通过继承JDK 中的 java.beans.PropertyEditorSupport 类来实现自己的编辑器类 ,该类用于实现将String 类型转换成您需要的数据类型.然后我们只需要在Sprin ...

  2. spring中自定义属性编辑器CustomEditorConfigurer

    http://gundumw100.iteye.com/blog/574440 什么是属性编辑器,作用? * 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入 spring已经 ...

  3. [Spring 深度解析]第3章 核心容器

    第3章 ◄核心容器► ​ 在上一章节中,我们大致了解了Spring框架,并学习了控制反转(IOC)和面向切面编程(AOP)两个重要的编程思想,这一章我们将开始学习Spring框架中的核心容器. 本章主 ...

  4. Spring Boot特性

    2019独角兽企业重金招聘Python工程师标准>>> SpringApplication SpringApplication类提供了一种从main()方法启动Spring应用的便捷 ...

  5. spring装配Bean过程

    主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...

  6. spring beans源码解读之--总结篇

    spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类. org.springframework.beans.ann ...

  7. mina与spring集成(翻译)

    源地址:http://mina.apache.org/mina-project/userguide/ch17-spring-integration/ch17-spring-integration.ht ...

  8. Spring - Java/J2EE Application Framework 应用框架 第 3 章 Beans, BeanFactory和ApplicationContext

    第 3 章 Beans, BeanFactory和ApplicationContext 3.1. 简介 在Spring中,两个最基本最重要的包是 org.springframework.beans 和 ...

  9. 框架:Spring之Aware相关接口

    一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...

最新文章

  1. FTP、HTTP断点续传和多线程的协议基础
  2. 教程:13、用户管理
  3. 寒门出贵子,传递正能量
  4. linux 信号传递函数,Linux Shell之七 函数应用
  5. CTFshow 命令执行 web64
  6. php 删除字符前缀,thinkphp3.2.3如何删除同一前缀的缓存(File)
  7. xp win7 linux 三系统下载,打造xp+linux+win7三系统教程.doc
  8. python乱码大赛_FishC工作室《零基础学python》全套课后题.doc
  9. 学生图书管理系统(附源代码及数据库)
  10. malformed header from script. Bad header的解决方法以及原因
  11. 3分钟短文:Laravel路子真野啊!路由昵称前缀中间件
  12. hrbust 哈理工oj 1633 word!word!【欧拉路、欧拉回路的有向图判断】
  13. Javascript:谈谈JS的全局变量跟局部变量(转zyz511919766)
  14. C语言中void的错误用法
  15. 7-1 求e的近似值 (15 分)
  16. 北京网站建设比较好的公司
  17. 【php基础入门】运算符、流程控制语句及单双引号区别和模块化编程的使用详解
  18. 深度学习——(12)Knowledge distillation(Demo)
  19. SpringCloud——Zookeeper
  20. 农村土地确权之例会纪要—— 新蔡县土地确权第十次例会及苍穹软件第二次培训纪要

热门文章

  1. 利用 iPhone X 的脸部识别能力为内容制作工作服务
  2. python 两台机器之间传递数据_Python:通过套接字在两台计算机之间发送数据
  3. 二次元萌妹在电车上顺拐啾咪为哪般?
  4. 可以装在手机里的3D姿态估计,模型尺寸仅同类1/7,平均关节位置误差却只有5厘米 | CPVR 2021...
  5. 岚图FREE入局之战,手握哪些底牌?
  6. 解读Java8中ConcurrentHashMap是如何保证线程安全的
  7. Office365 Exchange Hybrid No.15 DNS切换及邮件传输排错
  8. 用 TensorFlow 目标检测 API 发现皮卡丘!
  9. homebrew常用指令
  10. 推荐 查公司信息的一个网站