谈谈Java开发中的对象拷贝

在Java开发工作中,有很多时候我们需要将不同的两个对象实例进行属性复制,从而基于源对象的属性信息进行后续操作,而不改变源对象的属性信息。这两个对象实例有可能是同一个类的两个实例,也可能是不同类的两个实例,但是他们的属相名称相同。例如DO、DTO、VO、DAO等,这些实体的意义请查看DDD中分层架构。本文主要介绍几种对象拷贝的方法

1. 对象拷贝

对象拷贝分为深拷贝和浅拷贝。根据使用场景进行不同选择。在Java中,数据类型分为值类型(基本数据类型)和引用类型,值类型包括int、double、byte、boolean、char等简单数据类型,引用类型包括类、接口、数组等复杂类型。

深度拷贝和浅度拷贝的主要区别在于是否支持引用类型的属性拷贝,本文将探讨目前使用较多的几种对象拷贝的方案,以及其是否支持深拷贝和性能对比。

2. BeanUtils

2.1 apache的BeanUtils方案

使用org.apache.commons.beanutils.BeanUtils进行对象深入复制时候,主要通过向BeanUtils框架注入新的类型转换器,因为默认情况下,BeanUtils对复杂对象的复制是引用,例如:

public static void beanUtilsTest() throws Exception {// 注册转化器BeanUtilsBean.getInstance().getConvertUtils().register(new ArbitrationConvert(), ArbitrationDO.class);Wrapper wrapper = new Wrapper();wrapper.setName("copy");wrapper.setNameDesc("copy complex object!");wrapper.setArbitration(newArbitrationDO());Wrapper dest = new Wrapper();// 对象复制BeanUtils.copyProperties(dest, wrapper);// 属性验证wrapper.getArbitration().setBizId("1");System.out.println(wrapper.getArbitration() == dest.getArbitration());System.out.println(wrapper.getArbitration().getBizId().equals(dest.getArbitration().getBizId()));
}public class ArbitrationConvert implements Converter {@Overridepublic <T> T convert(Class<T> type, Object value) {if (ArbitrationDO.class.equals(type)) {try {return type.cast(BeanUtils.cloneBean(value));} catch (Exception e) {e.printStackTrace();}}return null;}
}

可以发现,使用org.apache.commons.beanutils.BeanUtils复制引用时,主和源的引用为同一个,即改变了主的引用属性会影响到源的引用,所以这是一种浅拷贝。

需要注意的是,apache的BeanUtils中,以下类型如果为空,会报错(org.apache.commons.beanutils.ConversionException: No value specified for  *)

/*** Register the converters for other types.* </p>* This method registers the following converters:* <ul>*     <li>Class.class - {@link ClassConverter}*     <li>java.util.Date.class - {@link DateConverter}*     <li>java.util.Calendar.class - {@link CalendarConverter}*     <li>File.class - {@link FileConverter}*     <li>java.sql.Date.class - {@link SqlDateConverter}*     <li>java.sql.Time.class - {@link SqlTimeConverter}*     <li>java.sql.Timestamp.class - {@link SqlTimestampConverter}*     <li>URL.class - {@link URLConverter}* </ul>* @param throwException <code>true if the converters should* throw an exception when a conversion error occurs, otherwise <code>* <code>false if a default value should be used.*/private void registerOther(boolean throwException) {register(Class.class,         throwException ? new ClassConverter()        : new ClassConverter(null));register(java.util.Date.class, throwException ? new DateConverter()        : new DateConverter(null));register(Calendar.class,      throwException ? new CalendarConverter()     : new CalendarConverter(null));register(File.class,          throwException ? new FileConverter()         : new FileConverter(null));register(java.sql.Date.class, throwException ? new SqlDateConverter()      : new SqlDateConverter(null));register(java.sql.Time.class, throwException ? new SqlTimeConverter()      : new SqlTimeConverter(null));register(Timestamp.class,     throwException ? new SqlTimestampConverter() : new SqlTimestampConverter(null));register(URL.class,           throwException ? new URLConverter()          : new URLConverter(null));}

当遇到这种问题是,可以手动将类型转换器注册进去,比如data类型:

public class BeanUtilEx extends BeanUtils { private static Map cache = new HashMap(); 
private static Log logger = LogFactory.getFactory().getInstance(BeanUtilEx.class); private BeanUtilEx() { 
} static { 
// 注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空 
ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class); 
ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.util.Date.class);  
ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null), java.sql.Timestamp.class); 
// 注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空 
} public static void copyProperties(Object target, Object source) 
throws InvocationTargetException, IllegalAccessException { 
// 支持对日期copy 
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source); } 

2.2 apache的PropertyUtils方案

PropertyUtils的copyProperties()方法几乎与BeanUtils.copyProperties()相同,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,PropertyUtils不支持这个功能,所以说BeanUtils使用更普遍一点,犯错的风险更低一点。而且它仍然属于浅拷贝。

Apache提供了 SerializationUtils.clone(T),T对象需要实现 Serializable 接口,他属于深克隆。

2.3 spring的BeanUtils方案

Spring中的BeanUtils,其中实现的方式很简单,就是对两个对象中相同名字的属性进行简单get/set,仅检查属性的可访问性。

public static void copyProperties(Object source, Object target) throws BeansException {copyProperties(source, target, (Class)null, (String[])null);}public static void copyProperties(Object source, Object target, Class<?> editable) throws BeansException {copyProperties(source, target, editable, (String[])null);}public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {copyProperties(source, target, (Class)null, ignoreProperties);}private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) throws BeansException {Assert.notNull(source, "Source must not be null");Assert.notNull(target, "Target must not be null");Class actualEditable = target.getClass();if(editable != null) {if(!editable.isInstance(target)) {throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");}actualEditable = editable;}PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);List ignoreList = ignoreProperties != null?Arrays.asList(ignoreProperties):null;PropertyDescriptor[] var7 = targetPds;int var8 = targetPds.length;for(int var9 = 0; var9 < var8; ++var9) {PropertyDescriptor targetPd = var7[var9];Method writeMethod = targetPd.getWriteMethod();if(writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());if(sourcePd != null) {Method readMethod = sourcePd.getReadMethod();if(readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {try {if(!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {readMethod.setAccessible(true);}Object ex = readMethod.invoke(source, new Object[0]);if(!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {writeMethod.setAccessible(true);}writeMethod.invoke(target, new Object[]{ex});} catch (Throwable var15) {throw new FatalBeanException("Could not copy property \'" + targetPd.getName() + "\' from source to target", var15);}}}}}}

可以看到, 成员变量赋值是基于目标对象的成员列表, 并且会跳过ignore的以及在源对象中不存在的, 所以这个方法是安全的, 不会因为两个对象之间的结构差异导致错误, 但是必须保证同名的两个成员变量类型相同.

3. dozer

Dozer(http://dozer.sourceforge.net/)能够实现深拷贝。Dozer是基于反射来实现对象拷贝,反射调用set/get 或者是直接对成员变量赋值 。 该方式通过invoke执行赋值,实现时一般会采用beanutil, Javassist等开源库。

简单引用网上的例子,大多都是基于xml的配置,具体请查看其它Blog:

package com.maven.demo;import java.util.HashMap;
import java.util.Map;import org.dozer.DozerBeanMapper;
import org.junit.Test;import static org.junit.Assert.assertEquals;public class Demo{/*** map->bean*/@Testpublic void testDozer1() {Map<String,Object> map = new HashMap();map.put("id", 10000L);map.put("name", "小兵");map.put("description", "帅气逼人");DozerBeanMapper mapper = new DozerBeanMapper();ProductVO product = mapper.map(map, ProductVO.class);assertEquals("小兵",product.getName());assertEquals("帅气逼人",product.getDescription());assertEquals(Long.valueOf("10000"), product.getId());}/*** VO --> Entity  (不同的实体之间,不同的属性字段进行复制)*/@Testpublic void testDozer2(){ProductVO product = new ProductVO();product.setId(10001L);product.setName("xiaobing");product.setDescription("酷毙了");DozerBeanMapper mapper = new DozerBeanMapper();ProductEntity productEntity = mapper.map(product, ProductEntity.class);assertEquals("xiaobing",productEntity.getProductName());}}

4.  MapStrcut

MapStrcut属于编译期的对象复制方案,它能够动态生成set/get代码的class文件 ,在运行时直接调用该class文件。该方式实际上扔会存在set/get代码,只是不需要自己写了。

@Mapper(componentModel = "spring")
public interface MonitorAppGroupIdcDTOMapper {MonitorAppGroupIdcDTOMapper MAPPER = Mappers.getMapper(MonitorAppGroupIdcDTOMapper.class);void mapping(MonitorAppGroupIdcDTO source, @MappingTarget MonitorAppGroupIdcDTO dest);
}

5. 自定义Pojoconvert

public J copyPojo( P src, J des) throws NoSuchMethodException,SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {if(src == null || des==null){return null;}String name = null ;String sufix = null;Class<?> cls = des.getClass() ;Method[] methods = cls.getMethods();for(Method m: methods){name = m.getName();if(name!=null && name.startsWith("set") && m.getParameterTypes().length==1){sufix = name.substring(3);m.getParameterTypes() ;Method getM = cls.getMethod("get"+sufix);m.invoke(des, getM.invoke(src));}}return des ;
}

没有那么多验证,不是很安全但是性能不错。

6. BeanCopier

@Testpublic void test_convert_entity_to_model_performance_use_beancopier(){List<ShopCouponEntity> entityList  = ...long start = System.currentTimeMillis();BeanCopier b = BeanCopier.create(ShopCouponEntity.class, ShopCouponModel.class, false);List<ShopCouponModel> modelList = new ArrayList<>();for (ShopCouponEntity src : entityList) {ShopCouponModel dest = new ShopCouponModel();b.copy(src, dest, null);modelList.add(dest);}System.out.printf("BeanCopier took time: %d(ms)%n",System.currentTimeMillis() - start);}
可以通过缓存BeanCopier的实例来提高性能。

BeanCopier b = getFromCache(sourceClass,targetClass); //从缓存中取long start = System.currentTimeMillis();List<ShopCouponModel> modelList = new ArrayList<>();for (ShopCouponEntity src : entityList) {ShopCouponModel dest = new ShopCouponModel();b.copy(src, dest, null);modelList.add(dest);}

7. fastjson和GSON

使用fastjson和GSON主要是通过对象json序列化和反序列化来完成对象复制,这里只是提供一种不一样的对象拷贝的思路,例子略。

8. 性能

对两种BeanUtils、Gson以及自定义Pojoconvert测试了性能

NewNovelMode des = null ;
NewNovelMode ori = buildModel();
Gson gson = new Gson();     
int count = 100000;
//org.springframework.beans.BeanUtils.copyProperties
long s = System.currentTimeMillis();
for(int i=0;i<count;i++){des = new NewNovelMode();org.springframework.beans.BeanUtils.copyProperties(ori, des);
}
System.out.println("springframework BeanUtils cost:"+(System.currentTimeMillis() - s));
//      System.out.println(new Gson().toJson(des));//org.apache.commons.beanutils.BeanUtils
s = System.currentTimeMillis();
for(int i=0;i<count;i++){des = new NewNovelMode();org.apache.commons.beanutils.BeanUtils.copyProperties(des, ori);
}
System.out.println("apache BeanUtils cost:"+(System.currentTimeMillis() - s));
//      System.out.println(new Gson().toJson(des));//gson转换
s = System.currentTimeMillis();
for(int i=0;i<count;i++){des = gson.fromJson(gson.toJson(ori), NewNovelMode.class);
}
System.out.println("gson cost:"+(System.currentTimeMillis() - s));
//      System.out.println(new Gson().toJson(des));//Pojo转换类
s = System.currentTimeMillis();
PojoUtils<NewNovelMode, NewNovelMode> pojoUtils = new PojoUtils<NewNovelMode, NewNovelMode>();
for(int i=0;i<count;i++){des = new NewNovelMode();pojoUtils.copyPojo(ori,des);
}
System.out.println("Pojoconvert cost:"+(System.currentTimeMillis() - s));
//      System.out.println(new Gson().toJson(des));

结果就不贴出来了,在这里总结一下

Spring的BeanUtils比较稳定,不会因为量大了,耗时明显增加,但其实基准耗时比较长;apache的BeanUtils稳定性与效率都不行,不可取;Gson,因为做两个gson转换,所以正常项目中,可能耗时会更少一些;PojoUtils稳定不如spring,但是总耗时优势明显,原因是它只是根据项目的需求,实现的简单的转换模板,这个代码在其它的几个工具类均有。

而在网上的其他Blog中(参见Reference),对Apache的BeanUtils、PropertyUtils和CGLIB的BeanCopier作了性能测试。

测试结果:

性能对比: BeanCopier > BeanUtils. 其中BeanCopier的性能高出另外两个100数量级。

综上推荐使用:

1. BeanUtils(简单,易用)

2. BeanCopier(加入缓存后和手工set的性能接近)

3. Dozer(深拷贝)

4. fastjson(特定场景下使用)


转自:https://my.oschina.net/hosee/blog/1483965


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/443322.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gitmaven命令

git命令 git diff #查看差异 git push origin feature/recover_pwd_bug #推送 git commit -m ‘perf #重置密码逻辑优化 git log #查看提交版本号 git reset --hard <版本号> #本地回退到相应的版本 git push origin <分支名> --force #远端的仓库也回退到相应…

【算法系列之一】二叉树最小深度

题目&#xff1a; 给定一个二叉树&#xff0c;找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3/ \9 20/ \15 7 返回它的最小深度 2. 答案&#xf…

【算法系列之二】反波兰式

问题&#xff1a; 用反波兰式表示算术表达式的值。 有效运算符是,-,*,/。每个操作数可以是一个整数或另一个表达式。 一些例子&#xff1a; ["2", "1", "", "3", "*"] -> ((2 1) * 3) -> 9["4", "13…

【算法系列之三】单链表反转

问题&#xff1a; 实现单链表反转 答案&#xff1a; 链表准备 class Node {private int Data;// 数据域private Node Next;// 指针域public Node(int Data) {// super();this.Data Data;}public int getData() {return Data;}public void setData(int Data) {this.Data D…

Java常见异常总结

1、java.lang.NullPointerException(空指针异常)   调用了未经初始化的对象或者是不存在的对象 经常出现在创建图片&#xff0c;调用数组这些操作中&#xff0c;比如图片未经初始化&#xff0c;或者图片创建时的路径错误等等。对数组操作中出现空指针&#xff0c; 即把数组的…

从数据库表中随机获取N条记录的SQL语句

Oracle: select * from (select * from tableName order by dbms_random.value) where rownum < N; MS SQLServer: select top N * from tableName order by newid(); My SQL: select * from tableName order by rand() limit N; 转自&#xff1a;http://blog.csdn.net/sent…

Linux下的MySQL安装及卸载

1.1 查看mysql的安装路径&#xff1a; [rootbogon ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql/usr/share/mysql /usr/share/man/man1/mysql.1.gz 1.2 查看mysql的安装包&#xff1a; [rootbogon ~]# rpm -qa|grep mysql mysql-community-client-5.6.26-2.…

mysql explain用法

explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。使用方法&#xff0c;在select语句前加上explain就可以了&#xff0c;如&#xff1a;explain select * from statuses_status where id11;创建测试表&#xff1a;CR…

Linux 性能检查命令总结

如果你的Linux服务器突然负载暴增&#xff0c;告警短信快发爆你的手机&#xff0c;如何在最短时间内找出Linux性能问题所在&#xff1f;

线程池的各种使用场景

&#xff08;1&#xff09;高并发、任务执行时间短的业务&#xff0c;线程池线程数可以设置为CPU核数1&#xff0c;减少线程上下文的切换 &#xff08;2&#xff09;并发不高、任务执行时间长的业务要区分开看&#xff1a; a&#xff09;假如是业务时间长集中在IO操作上…

Java线程面试题 Top 50

不管你是新程序员还是老手&#xff0c;你一定在面试中遇到过有关线程的问题。Java语言一个重要的特点就是内置了对并发的支持&#xff0c;让Java大受企业和程序员的欢迎。大多数待遇丰厚的Java开发职位都要求开发者精通多线程技术并且有丰富的Java程序开发、调试、优化经验&…

深入理解Semaphore

使用 Semaphore是计数信号量。Semaphore管理一系列许可证。每个acquire方法阻塞&#xff0c;直到有一个许可证可以获得然后拿走一个许可证&#xff1b;每个release方法增加一个许可证&#xff0c;这可能会释放一个阻塞的acquire方法。然而&#xff0c;其实并没有实际的许可证这…

【算法系列之四】柱状图储水

题目&#xff1a; 给定一个数组&#xff0c;每个位置的值代表一个高度&#xff0c;那么整个数组可以看做是一个直方图&#xff0c; 如果把这个直方图当作容器的话&#xff0c;求这个容器能装多少水 例如&#xff1a;3&#xff0c;1&#xff0c;2&#xff0c;4 代表第一个位…

盐城大数据产业园人才公寓_岳西大数据产业园规划设计暨建筑设计方案公布,抢先一睹效果图...

近日&#xff0c;岳西县大数据产业园规划设计暨建筑设计方案公布。岳西县大数据产业园项目总占地面积17014.10㎡(约合25.52亩)&#xff0c;拟建总建筑面积约为61590.84㎡(地上建筑面积39907.49㎡&#xff0c;地下建筑面积21602.35㎡)。以“科技圆环”为主题&#xff0c;组建出一…

【算法系列之五】对称二叉树

给定一个二叉树&#xff0c;检查它是否是镜像对称的。 例如&#xff0c;二叉树 [1,2,2,3,4,4,3] 是对称的。 1/ \2 2/ \ / \ 3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1/ \2 2\ \3 3 说明: 如果你可以运用递归和迭代两种方法解决这个问题&a…

【算法系列之六】两整数之和

不使用运算符 和 - &#xff0c;计算两整数 a 、b 之和。 示例 1: 输入: a 1, b 2 输出: 3示例 2: 输入: a -2, b 3 输出: 1 方法一&#xff1a;递归 public static int getSum1(int a, int b) {if ((a & b) ! 0) { // 判断是否有进位return getSum1(a ^ b, (a &…

cuda默认函数与c++冲突_好程序员Python教程系列-第8讲:函数和模块

好程序员Python教程系列-第8讲&#xff1a;函数和模块&#xff0c;在讲解本章节的内容之前&#xff0c;我们先来研究一道数学题&#xff0c;请说出下面的方程有多少组正整数解。事实上&#xff0c;上面的问题等同于将8个苹果分成四组每组至少一个苹果有多少种方案&#xff0c;所…

【算法系列之七】合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例&#xff1a; 输入&#xff1a;1->2->4, 1->3->4 输出&#xff1a;1->1->2->3->4->4/*** Definition for singly-linked list.* public cla…

mfc让图片与按钮一起_对许多张图片进行批量裁剪,看看我是如何快速做到的

概要&#xff1a;当我们需要对很多图片进行批量裁剪时&#xff0c;以往的办法是自己一张一张图片去操作&#xff0c;非常麻烦。有没有这样一个工具&#xff0c;能够帮我们批量进行处理呢&#xff1f;之前小编在网上找了非常多的软件&#xff0c;一个一个地安装试用&#xff0c;…

【算法系列之八】删除链表的倒数第N个节点

给定一个链表&#xff0c;删除链表的倒数第 n 个节点&#xff0c;并且返回链表的头结点。 示例&#xff1a; 给定一个链表: 1->2->3->4->5, 和 n 2.当删除了倒数第二个节点后&#xff0c;链表变为 1->2->3->5.说明&#xff1a; 给定的 n 保证是有效的…