BeanUtils.copyProperties() 用法

来源:互联网 发布:域名主机 编辑:程序博客网 时间:2024/05/22 01:53

最近在项目中接触到了BeanUtils.copyProperties(),之前没怎么用过这个方法,在网上搜索了一下,发现

一个Spring的

一个Apache的


Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg

:http://www.open-open.com/lib/view/open1404998048200.html



还有一个PropertyUtils.copyProperties()的方法,下面对这两个方法做一下总结。另外,在使用的时候,程序抛出了异常,经过分析得知,异常的原因是因为赋值的对象中有一个时间类型(该属相采用java.util.Date的类型),程序无法通过,下面主要分析这两个问题。


方法/步骤

  1. 第一步: BeanUtils.copyProperties()与PropertyUtils.copyProperties()



    1、 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同)。

    2、 BeanUtils.copyProperties(obj1,obj2); 经常闹混不知道是谁给谁赋值,无意中先到"后付前"这个词来帮助自己记忆这个功能。即将obj2的值赋值给obj1。

    3、 如果2中实例obj2为空对象,即值new了他的实例并没有赋值的话obj1对应的属性值也会被设置为置。

    4、BeanUtils与PropertyUtils对比(这里对比copyProperties方法)

    PropertyUtils的copyProperties()方法几乎与BeanUtils.copyProperties()相同,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,BeanUtils 不支持这个功能,但是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。如果开发中Date类型采用util而非sql.Date程序会抛出argument mistype异常。



  2. 第二步:扩展BeanUtils支持时间类型转换


    import java.lang.reflect.InvocationTargetException;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.ConvertUtils;

    /**
    * 重写BeanUtils.copyProperties
    *
    * @author monkey
    */
    public class BeanUtilsExtends extends BeanUtils {
       static {
           ConvertUtils.register(new DateConvert(), java.util.Date.class);
           ConvertUtils.register(new DateConvert(), java.sql.Date.class);
       }

       public static void copyProperties(Object dest, Object orig) {
           try {
               BeanUtils.copyProperties(dest, orig);
           } catch (IllegalAccessException ex) {
               ex.printStackTrace();
           } catch (InvocationTargetException ex) {
               ex.printStackTrace();
           }
       }
    }

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import org.apache.commons.beanutils.Converter;

    /**
    * 重写日期转换
    *
    * @author houzhiqing
    */
    public class DateConvert implements Converter {

       public Object convert(Class arg0, Object arg1) {
           String p = (String) arg1;
           if (p == null || p.trim().length() == 0) {
               return null;
           }
           try {
               SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               return df.parse(p.trim());
           } catch (Exception e) {
               try {
                   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                   return df.parse(p.trim());
               } catch (ParseException ex) {
                   return null;
               }
           }

       }

    }

  3. 源码如下:

  4. public static void copyProperties(Object source, Object target) throws BeansException {copyProperties(source, target, null, null);}/** * Copy the property values of the given source bean into the given target bean, * only setting properties defined in the given "editable" class (or interface). * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * <p>This is just a convenience method. For more complex transfer needs, * consider using a full BeanWrapper. * @param source the source bean * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @throws BeansException if the copying failed * @see BeanWrapper */


3 0
原创粉丝点击