org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的区别

来源:互联网 发布:标志设计软件 编辑:程序博客网 时间:2024/06/05 12:34

org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作用是将一个Bean对象中的数据封装到另一个属性结构相似的Bean对象中

1)两者的copyProperties方法参数位置不同

  org.springframework.beans.BeanUtils:  copyProperties(sourceDemo, targetDemo)

  org.apache.commons.beanutils.BeanUtils:  copyProperties(targetDemo, sourceDemo)

2)要求两个Bean的属性名相同,且有对应的setXxx方法和getXxx方法。其实底层原理是使用sourceDemo的getXxx方法和targetDemo的setXxx方法

3)sourceDemo有的属性而targetDemo没有的属性,不会封装到targetDemo对象中;

   targetDemo有的属性而sourceDemo没有的属性,会封装到targetDemo中,数据为默认值(注意基本类型默认值与引用类型默认值不同)

4)类型转换问题

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

  b)org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils对于String和Date类型转换的情况是不同的。