BeanUtils.copyProperties() 用法--部分转

时间:2023-03-08 22:03:59

把一个类对象copy到另一个类对象(这两个可以不同)。

1、org.apache.commons.beanutils.BeanUtils.copyProperties(dest, src) 
org.springframework.beans.BeanUtils.copyProperties(src, dest) 
注意:2者的入参顺序不同,严重

BeanUtils.copyProperties(source,target);

1、

spring的BeanUtils.copyProperties 要求property类型严格一致,否则就出错 
比如srcBean有一个属性是Short, destBean有一个同名属性,但是类型是Integer,就会出错。

报这个错误:

Caused by: org.springframework.beans.FatalBeanException: Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException: argument type mismatch

2、

4、org.apache.commons.beanutils.BeanUtils.copyProperties 会把null的Integer转换成0,再复制