BeanUtils与PropertyUtils区别

来源:互联网 发布:淘宝怎么知道卖家电话 编辑:程序博客网 时间:2024/05/16 03:35
BeanUtilsPropertyUtils区别以及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 BeanUtilsmethods (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 theConvertUtils.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中的dateString
Address2 addr2=new Address2(); //Address1中的datejava.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().toLocaleString()); 
0 0
原创粉丝点击