目前较为流行的用于对象拷贝的工具类:
Apache的两个版本:(反射机制)
org.apache.commons.beanutils.PropertyUtils.copyProperties(Object dest, Object orig)
org.apache.commons.beanutils.BeanUtils.copyProperties(Object dest, Object orig)
spring版本:(反射机制)
org.springframework.beans.BeanUtils.copyProperties(Object source, Object target, Class editable, String[] ignoreProperties)
cglib版本:(使用动态代理,效率高)
net.sf.cglib.beans.BeanCopier.copy(Object paramObject1, Object paramObject2, Converter paramConverter)
有网友对这四种工具进行性能测试结果如下:
10次 10000次
Apache-BeanUtils 53 230
Apache-PropertyUtils 4 91
Spring-BeanUtils 11 31
BeanCopier 0 1
其中BeanCopier效率最高,只是在创建代理类的时候比较耗时,可以使用单例,
Apache-BeanUtils效率最低,只是它能在性能名形同,类型不匹配是进行初级转换,而且在使用中发现copy long类型数据时,如果源对象该字段为空,则目标字段自动赋值为0
spring-beanutils在次数多时性能比较好
apache-propertyutils表现则呈现线性增长的趋势