一、对象属性拷贝工具类
”天下武功,唯快不破“。在互联网行业中体现的更加淋淋尽致。我们在业务系统会经常遇到业务对象间属性的拷贝,对如外接口一般都使用特定的DTO对象,而不会使用领域模型,以避免两者的变动互相影响。我们不仅要关注“快”,还要注重CPU的稳定即避免CPU使用的大起大落现象。如何高效完成属性的拷贝并降低对CPU的使用率或避免CPU的抖动。
相关博文已经有很多,为什么还要自己在一篇类似的哪?原因有二:一是加深理解二是比较各自优劣。目前对象间属性的拷贝常用的方法大致如下:
- 手动拷贝(set)
- 动态代理
cglib版本:net.sf.cglib.beans.BeanCopier.copy(Object from, Object to, Converter converter)
- 反射机制
Spring版本:org.springframework.beans.BeanUtils.copyProperties(Object source, Object target)
Apache版本:org.apache.commons.beanutils.PropertyUtils.copyProperties(Object dest, Object orig)
org.apache.commons.beanutils.BeanUtils.copyProperties(Object dest, Object orig)
DozerMapper
二、实践说明性能优劣
1、环境
WIN7 i5,12G内存,
JVM:
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
依赖jar及版本
2、代码结构
package test; /** * @author wy * */ public interface IMethodCallBack { public String getMethodName(); public DestBean callBack(SourceBean sourceBean) throws Exception; }