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.
A 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--转的更多相关文章
- spring源码:学习线索(li)
一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...
- Spring ApplicationContext 简解
ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等. configure locations:(C ...
- Spring学习笔记之二----基于XML的Spring AOP配置
在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...
- spring入门教程——笔记
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- Spring知识点总结大全(1)
1.Spring的分层结构 1.Presentation layer(表示层) (1) 表示逻辑(生成界面代码) (2) 接收请求 (3) 处理业务层抛出的异常 (4) 负责规则验证(数据格式,数据非 ...
- springMvc源码学习之:spring源码总结
转载自:http://www.cnblogs.com/davidwang456/p/4213652.html spring beans下面有如下源文件包: org.springframework.be ...
- Spring(三)Bean继续入门
一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...
- Spring的属性编辑器
bean类 import java.util.Date; public class Bean { private Date date; public Date getDate() { return d ...
- Spring的BeanFactoryPostProcessor和BeanPostProcessor
转载:http://blog.csdn.net/caihaijiang/article/details/35552859 BeanFactoryPostProcessor和BeanPostProces ...
随机推荐
- URAL 1457. Heating Main
space=1&num=1457">1457. Heating Main Time limit: 1.0 second Memory limit: 64 MB Backgrou ...
- yolo源码解析(2):处理图片
首先安装ffmpeg, 参考https://blog.csdn.net/lwgkzl/article/details/77836207 然后将视频切分为图片, 参考:https://zhuanlan. ...
- FPGA中亚稳态——让你无处可逃
1. 应用背景 1.1 亚稳态发生原因 在FPGA系统中,如果数据传输中不满足触发器的Tsu和Th不满足,或者复位过程中复位信号的释放相对于有效时钟沿的恢复时间(recovery ti ...
- m_Orchestrate learning system---八、下拉列表(select标签)如何实现链接功能
m_Orchestrate learning system---八.下拉列表(select标签)如何实现链接功能 一.总结 一句话总结:option的值就是链接地址,选择事件为指向选中的option的 ...
- 5分钟学会 CSS Grid 布局
欢迎加入前端交流群交流知识&&获取视频资料:749539640 这是一篇快速介绍网站未来布局的文章. Grid 布局是网站设计的基础,CSS Grid 是创建网格布局最强大和最简单的工 ...
- correct ways to define variables in python
http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python later say ...
- [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)
题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...
- C#各个版本中的新增特性详解【转】
序言 自从2000年初期发布以来,c#编程语言不断的得到改进,使我们能够更加清晰的编写代码,也更加容易维护我们的代码,增强的功能已经从1.0搞到啦7.0甚至7.1,每一次改过都伴随着.NET Fram ...
- Linux命令locate
centos安装locate命令 centos6.3刚初始化安装完毕,有个配置文件不知道存在什么地方,想用locate命令来查找下,发现系统提示,找不到该命令.以前经常用的命令为什么找不到了呢???原 ...
- 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...