org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法和自己手动赋值属性

1)参数位置不同

       springframework.beans.BeanUtils.copyProperties(A,B):把A对象的属性值赋给B对象相应的属性

        commons.beanutils.BeanUtils.copyProperties(A,B):把B对象的属性值赋给A对象相应的属性

2)类型转换问题


  a)基本类型与其对应的封装类型可以相互转换

      b)
springframework.beans.BeanUtils: util.Date---->String   不可以
                                 String------>util.Date   不可以
 commons.beanutils.BeanUtils:    util.Date---->String   可以

                                 String------>util.Date 抛DateConverter does not support default String to 'Date' conversion.

与手动对象copy相比:

  1:当需要保留B对象的某些属性时,这些属性在A对象中没有赋值,那么传给B的相应值就会为空
  2.相对于手动赋值非常耗时,使用copyProperties耗时几百毫秒,而手动测的1毫秒都不到

猜你喜欢

转载自blog.csdn.net/csndryl/article/details/79841375