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 ...
随机推荐
- unity3d进程通信利用WM_COPYDATE和HOOK
hello,近期用unity做了进程通信,应该是和c++的PC端实现通信,才開始一头雾水,后来实现了才知道好繁杂......先感谢对我提供帮助的百度,谷歌以及游戏圈的大大们. 在进程通信中非常多方法, ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- android 推断手机是否支持前置摄像头
话不多说 直接上代码, @SuppressLint("NewApi") public static boolean isSupportFrontCamera() { if (!ha ...
- HDU 5228 ZCC loves straight flush( BestCoder Round #41)
题目链接:pid=5228">ZCC loves straight flush pid=5228">题面: pid=5228"> ZCC loves s ...
- Cocos2d-x可以实现的动画效果
动作(Actions)move移动:moveto/moveby 从一个位置移动到另外一个位置 从一个位置移动多少数量级rotate旋转:rotateto/rotateby 从一个角度旋转到另外一个角度 ...
- poj3352Road Construction 边双连通+伪缩点
/* 对于边双连通分支,求法更为简单. 仅仅需在求出全部的桥以后,把桥边删除.\ 原图变成了多个连通块,则每一个连通块就是一个边双连通分支. 桥不属于不论什么 一个边双连通分支,其余的边和每一个顶点都 ...
- ES task管理
Task Management API The Task Management API is new and should still be considered a beta feature. Th ...
- linux 内存不足时候 应该及时回收page cache
另一起问题是24G内存的系统,空闲内存已经不到50M 1. 确认该系统的版本是64位 # uname -a Linux gxgd-nms-app 2.6.18-194.el5xen #1 SMP Tu ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- 洛谷P2118 比例简化(暴力)
题目描述 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有1498 人,反对的有 902人,那么赞同与反对的比例可以简单的记为1498:902. 不过,如 ...