Spring中Converter和Formatter的异同点
来源:互联网 发布:网络互动直播新闻 编辑:程序博客网 时间:2024/06/14 02:34
Spring中Converter和Formatter,都是用来做数据转换的。他们都是在Spring 3.0才出现的。之前使用的数据处理类是PropertyEditor。可是PropertyEditor存在的问题是他不是线程安全的,同时他只能处理String到其他类型的转换。其实应用中需要转换的地方还蛮常见的,比如日期类型转成指定格式再输出等。先来来看下这两个接口都有哪些方法:
Converter中只有一个方法convert
public interface Converter<S, T> {/** * Convert the source object of type {@code S} to target type {@code T}. * @param source the source object to convert, which must be an instance of {@code S} (never {@code null}) * @return the converted object, which must be an instance of {@code T} (potentially {@code null}) * @throws IllegalArgumentException if the source cannot be converted to the desired target type */T convert(S source);}
Formatter继承了Printer和Parser接口
public interface Formatter<T> extends Printer<T>, Parser<T> {},Printer用来打印转换后的对象
public interface Printer<T> {/** * Print the object of type T for display. * @param object the instance to print * @param locale the current user locale * @return the printed text string */String print(T object, Locale locale);},Parser用来从String类型转换为指定类型值
public interface Parser<T> {/** * Parse a text String to produce a T. * @param text the text string * @param locale the current user locale * @return an instance of T * @throws ParseException when a parse exception occurs in a java.text parsing library * @throws IllegalArgumentException when a parse exception occurs */T parse(String text, Locale locale) throws ParseException;}
从方法上,可以看分出2个接口的一些区别:Converter可以从任意源类型,转换为任意目标类型。而Formatter则是从String类型转换为任务目标类型,有点类似PropertyEditor。可以感觉出Converter是Formater的超集,实际上在Spring中Formatter是被拆解成PrinterConverter和ParserConverter,然后再注册到ConverterRegistry,供后续使用。这块后面再写文章说明:)
0 0
- Spring中Converter和Formatter的异同点
- Spring MVC 学习指南--- 第六章 converter 和formatter
- SQL Server中truncate、delete和drop的异同点
- VB.NET 中Structure和Class的异同点
- .net(c#)中Array和ArrayList的异同点
- 汇编中addr和offset的异同点
- css中.class和id选择器的异同点
- Java中sleep和wait方法的异同点
- JavaScript中apply()和call()的异同点
- SQL查询中having和where的异同点
- matlab中floor和ceil的异同点
- java中String和StringBuffer的异同点
- java中抽象类和接口的异同点
- Python中input 和raw_input的异同点
- SQL Server中truncate、delete和drop的异同点
- 简单分析下SPring MVC和Asp.net MVC的区别和异同点
- Array和ArrayList的异同点
- Java 和 C# 之间的异同点
- Windows安装MinGW解决方案
- SQL SERVER中事务的ACID
- Linux yum和apt-get的使用和区别
- uv位移动画
- 集合框架总结1
- Spring中Converter和Formatter的异同点
- LeetCode 123. Best Time to Buy and Sell Stock III(动态规划)
- jQuery中Ajax
- CodeForces
- 有向图的强连通分量(Tanjan)
- JDBC详解
- windows mysql命令导入数据库报错
- 27. Remove Element
- 集合框架总结2