Spring的BeanUtils的copyProperties方法需要注意的点


这两天做项目,用到了BeanUtils.copyProperties()这个方法,而在两个不同的类中使用到这个方法,但不知怎么的,copy属性总是出问题,最后排查终于找到原因。

BeanUtils.copyProperties(productInfo,orderDetail); //把属性拷贝过来

有两个类中包含有BeanUtils,且都有copyProperties方法,一个类为org.springframework.beans.BeanUtils,另一个是org.apache.commons.beanutils.BeanUtils,这两个类在不同的包下面,而这两个类的copyProperties()方法里面传递的参数赋值是相反的。

例如:

productInfo,orderDetail为两对象

BeanUtils.copyProperties(productInfo,orderDetail);

若BeanUtils是org.springframework.beans.BeanUtils,则是将productInfo中的属性拷贝到orderDetail;

若BeanUtils是org.apache.commons.beanutils.BeanUtils,则是将orderDetail中的属性拷贝到productInfo

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。