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

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

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

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

例如:

productInfoorderDetail为两对象

BeanUtils.copyProperties(productInfo,orderDetail);

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

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

原文地址:https://www.cnblogs.com/xiaozhengtongxue/p/13442780.html