BeanUtils与PropertyUtils区别
来源:互联网 发布:淘宝怎么知道卖家电话 编辑:程序博客网 时间:2024/05/16 03:35
BeanUtils与PropertyUtils区别以及java.util.Date发生异常问题:
除BeanUtils外还有一个名为PropertyUtils的工具类,它也提供copyProperties()方法,作用与BeanUtils的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,而前者不支持这个功能,但是速度会更快一些。BeanUtils支持的转换类型如下:
* java.lang.BigDecimal
* java.lang.BigInteger
* boolean and java.lang.Boolean
* byte and java.lang.Byte
* char and java.lang.Character
* java.lang.Class
* double and java.lang.Double
* float and java.lang.Float
* int and java.lang.Integer
* long and java.lang.Long
* short and java.lang.Short
* java.lang.String
* java.sql.Date
* java.sql.Time
* java.sql.Timestamp
这里要注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。
解决方法一:如果变为java.sql.Date这样用BeanUtils用可以和String进行copy property了.
Address1 addr3=new Address1();
System.out.println(StringUtils.center("test", 56,"-"));
addr2.setDate(Date.valueOf("2007-5-9"));
// PropertyUtils.copyProperties(addr2, addr3);
BeanUtils.copyProperties(addr3, addr2);
System.out.println(addr3.getDate());
解决方法二:自定义Converter的方法:Defining Your Own Converters
The
ConvertUtils
class supports the ability to define and register your own String --> Object conversions for any given Java class. Once registered, such converters will be used transparently by all of the BeanUtils
methods (including populate()
). To create and register your own converter, follow these steps:- Write a class that implements the Converter interface. The
convert()
method should accept thejava.lang.Class
object of your application class (i.e. the class that you want to convert to, and a String representing the incoming value to be converted. - At application startup time, register an instance of your converter class by calling the
ConvertUtils.register()
method.
代码如下:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.Converter;
publicclass CustomerDateConverter implements Converter {
privatefinalstatic SimpleDateFormat DATE_FORMATE_SHOW = new SimpleDateFormat("yyyy-MM-dd");
public Object convert(Class type, Object value){
// TODO Auto-generated method stub
if (type.equals(java.util.Date.class) ) {
try {
returnDATE_FORMATE_SHOW.parse(value.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
returnnull;
}
}
public java.lang.Object convert(java.lang.Class type,
java.lang.Object value)
Convert the specified input object into an output object of the specified type.
Parameters:
type - Data type to which this value should be converted (要转换的类型)
value - The input value to be converted (输入的值)
Returns:
The converted value
测试代码:
System.out.println(StringUtils.center("test", 56,"-"));
Address1 addr1=new Address1(); //Address1中的date是String
Address2 addr2=new Address2(); //Address1中的date是java.util.Date
Addr1.setDate("2007-2-6");
CustomerDateConverter dateConverter = new CustomerDateConverter ();
ConvertUtils.register(dateConverter,Date.class);
//上面两句是关键!
BeanUtils.copyProperties(addr2, addr1);
System.out.println(addr2.getDate(). 0 0
- BeanUtils与PropertyUtils区别
- BeanUtils.describe与PropertyUtils.describe区别
- BeanUtils和PropertyUtils区别
- BeanUtils和PropertyUtils区别
- BeanUtils.copyProperties 与 PropertyUtils.copyProperties 用法及区别
- BeanUtils.copyProperties 与 PropertyUtils.copyProperties 用法及区别
- BeanUtils.copyProperties 与 PropertyUtils.copyProperties 用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别[转载]
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别(转)
- BeanUtils.copyProperties 与 PropertyUtils.copyProperties 用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别[转载]
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- Linux常用命令
- [LeetCode] Best Time to Buy and Sell Stock II
- 海思H.264编解码芯片Hi3510的原理和应用
- 关于无线PCB中 中50欧姆的特性阻抗的注意事项
- 【体验分享】Apple Watch怎样才能更好用?
- BeanUtils与PropertyUtils区别
- SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN
- kamailio4.2.6 安装和配置教程
- 备忘----创建套接字进行地址和端口进行网络序转换时使用ipv4和ipv6都通用的函数
- Robot Framework 快速入门
- 【树链剖分】【NOI 2015】【bzoj 4196】软件包管理器
- iOS小白进阶之旅(一)
- jq和css3图片拉近效果
- 使用HttpClientdopost编写 客户端、服务器与数据库Demo