java 深拷贝工具类 dozer

本文介绍了一个针对Dozer库进行封装的实用工具类,该工具类简化了Bean之间的映射过程,支持单个对象及集合的转换,并提供了值复制功能。
package com.wafersystems.mina.util;


import java.util.Collection;
import java.util.List;


import org.dozer.DozerBeanMapper;


import com.google.common.collect.Lists;


/**
 * 简单封装Dozer, 实现深度转换Bean<->Bean的Mapper.实现:
 * 
 * 1. 持有Mapper的单例. 
 * 2. 返回值类型转换.
 * 3. 批量转换Collection中的所有对象.
 * 4. 区分创建新的B对象与将对象A值复制到已存在的B对象两种函数.
 * 
 * @author wangyun
 */
public final class BeanMapperUtils {


private BeanMapperUtils() {
}


/**
* 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
*/
private static DozerBeanMapper dozer = new DozerBeanMapper();


/**
* 基于Dozer转换对象的类型.
*/
public static <T> T map(Object source, Class<T> destinationClass) {
return dozer.map(source, destinationClass);
}


/**
* 基于Dozer转换Collection中对象的类型.
*/
public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
List<T> destinationList = Lists.newArrayList();
for (Object sourceObject : sourceList) {
T destinationObject = dozer.map(sourceObject, destinationClass);
destinationList.add(destinationObject);
}
return destinationList;
}


/**
* 基于Dozer将对象A的值拷贝到对象B中.
*/
public static void copy(Object source, Object destinationObject) {
dozer.map(source, destinationObject);
}
}
### Java 中用于对象数据拷贝的工具类Java中,除了通过`Cloneable`接口和手动编写克隆逻辑外,还有一些常用的第三方库可以帮助简化深浅拷贝的操作。 #### Apache Commons Lang 库中的 `SerializationUtils` Apache Commons Lang 提供了一个非常方便的方法来进行深拷贝——`SerializationUtils.clone()`。此方法利用序列化机制来完成深拷贝操作[^1]: ```java import org.apache.commons.lang3.SerializationUtils; public class DeepCopyExample { public static void main(String[] args) throws CloneNotSupportedException { Student originalStudent = new Student("张三", 20); // 使用 SerializationUtils 进行深拷贝 Student clonedStudent = SerializationUtils.clone(originalStudent); System.out.println(clonedStudent.equals(originalStudent)); // true, 如果重写了equals方法则比较内容一致 // 修改原始对象不会影响副本 originalStudent.setName("李四"); System.out.println("Original Name: " + originalStudent.getName()); System.out.println("Cloned Name: " + clonedStudent.getName()); } } ``` 这种方法适用于实现了`Serializable`接口的对象,并且能够很好地处理复杂嵌套结构下的属性复制问题。 #### Dozer 工具库 Dozer 是另一个流行的映射框架,它不仅支持简单的POJO之间字段的一一对应转换,还特别擅长于执行复杂的自定义映射规则以及实现深拷贝功能: ```xml <!-- Maven依赖 --> <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency> ``` ```java import org.dozer.DozerBeanMapper; import java.util.Arrays; public class DeepCopyWithDozer { private static final DozerBeanMapper mapper = new DozerBeanMapper(Arrays.asList("mapping-file.xml")); public static void main(String[] args) throws Exception { Student source = new Student("王五", 28); Address address = new Address(); address.setCity("Beijing"); source.setAddress(address); // 执行深拷贝 Student target = mapper.map(source, Student.class); // 测试独立性 source.getAddress().setCity("Shanghai"); System.out.println(target.getAddress().getCity()); // 输出 Beijing } } @Data @NoArgsConstructor @AllArgsConstructor static class Student implements Serializable{ private String name; private int age; private transient Address address; // 注意这里使用transient修饰符防止地址也被序列化 } @Data @NoArgsConstructor @AllArgsConstructor static class Address implements Serializable{ private String city; } ``` 需要注意的是,在某些情况下可能需要配置额外的地图文件或注解以便让Dozer知道如何正确地映射特定类型的成员变量。 #### Gson 或 Jackson JSON 解析器 对于那些不愿意引入大型依赖项的应用程序来说,也可以考虑采用JSON解析的方式来做深拷贝。这种方式简单易懂,但性能相对较低一些: ```java // 使用Gson做深拷贝的例子 import com.google.gson.Gson; public class JsonDeepCopyExample { public static <T> T deepCopy(T object, Class<T> clazz){ if (object == null) return null; Gson gson = new Gson(); String jsonStr = gson.toJson(object); return gson.fromJson(jsonStr,clazz); } public static void main(String[] args)throws Exception { Student student = new Student("赵六", 30); Student copyOfStudent = deepCopy(student, Student.class); System.out.println(copyOfStudent); } } ``` 以上几种方案各有优劣,开发者可以根据实际需求选择最适合的一种方式进行开发工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值