在Java中常见的对象拷贝方式是使用cglib,Cglib的性能是目前公认最好的,用于解决Bean拷贝的性能问题
下面是以cglib封装的工具类实现对象和集合的拷贝
一、引入依赖二、创建CglibUtil工具类cglib cglib3.3.0 compile
其中包含对象和集合的拷贝,还封装了Bean转Map的方法
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReflectUtil;
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.core.Converter;
import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class CglibUtil {
public static T copy(Object source, Class targetClass) {
return copy(source, targetClass, null);
}
public static T copy(Object source, Class targetClass, Converter converter) {
final T target = ReflectUtil.newInstanceIfPossible(targetClass);
copy(source, target, converter);
return target;
}
public static void copy(Object source, Object target) {
copy(source, target, null);
}
public static void copy(Object source, Object target, Converter converter) {
Assert.notNull(source, "Source bean must be not null.");
Assert.notNull(target, "Target bean must be not null.");
final Class> sourceClass = source.getClass();
final Class> targetClass = target.getClass();
boolean conIsNull = converter == null ? false : true;
final BeanCopier beanCopier = BeanCopier.create(sourceClass, targetClass, conIsNull);
beanCopier.copy(source, target, converter);
}
public static List copyList(Collection source, Supplier target) {
return copyList(source, target, null, null);
}
public static List copyList(Collection source, Supplier target, Converter converter) {
return copyList(source, target, converter, null);
}
public static List copyList(Collection source, Supplier target, BiConsumer callback) {
return copyList(source, target, null, callback);
}
public static List copyList(Collection source, Supplier target, Converter converter, BiConsumer callback) {
return source.stream().map(s -> {
T t = target.get();
copy(s, t, converter);
if (callback != null) {
callback.accept(s, t);
}
return t;
}).collect(Collectors.toList());
}
public static BeanMap toMap(Object bean) {
return BeanMap.create(bean);
}
}
三、测试拷贝对象和集合
拷贝对象测试,再将拷贝的对象和源对象放入集合中对拷贝集合测试
public static void main(String[] args) {
//创建对象
TUser tUser = new TUser();
tUser.setName("李四");
//拷贝对象
TUser tUser2 = CglibUtil.copy(tUser,TUser.class);
//打印结果
System.out.println(tUser2.getName());
//创建集合
List list1 = new ArrayList<>();
list1.add(tUser);
list1.add(tUser2);
//拷贝集合
List list2 = CglibUtil.copyList(list1,TUser::new);
//打印结果
System.out.println(JSONObject.toJSONString(list2));
}
结果打印
四、注意点CglibUtil.copy拷贝对象的时候,目标对象被lombok注解@Accessors(chain = true)修饰报空指针
在拷贝对象上不可用注解 @Accessors ,反射获取不到对应的setter,因为这个链式生成的代码带了返回值,此注解会将set方法返回值改变



