点赞收藏加关注是我创作的最大动力
大家可能用过很多种方式拷贝bean,一开始阿帕奇的BeanUtil,Spring里的BeanUtil、MapStruct等等,种类繁多,它们各有各的实现方式,有的是通过反射,有的是生成映射,有的是生成字节码。
可以说,每种方式各有各的优缺点(意思就是我不评价它们好坏),今天我只说cglib
中的BeanCopier
,它有什么特点呢:
- 生成字节码,生成调用set get的方法,所以效率极高(其实生成的代码就是
B.setXx(A.getAx())
) - 针对属性名一致但类型不一致的情况,支持自定义转换
- 仅支持符合
javaBean
规范的属性 - 值拷贝,也就是说只有最外层的对象是新的,属性都是用的原来那个对象的值(不是深拷贝)
没错,它最大的特点就是,生成字节码。
我们看一下它的用法:
BeanCopier beanCopier = BeanCopier.create(sourceClass, targetClass, false);
beanCopier.copy(source, target, null);
第一步,生成BeanCopier
实例(一个继承BeanCopier
的子类,是cglib
生成出来的)。
第二步,调用copy
方法。
如果我们把BeanCopier
实例缓存起来,那么这个copy的方法,速度就很快了(毕竟只有getter和setter),另外,大家有没有想过一点,如果需要拷贝的对象,目标类型没有无参构造,那怎么办?
这里介绍一下ObjenesisStd
(spring里已经集成了这个),它可以很好的解决这个问题。
官网文档是这么介绍的:Objenesis
使用多种方法来尝试实例化对象,具体取决于对象的类型,JVM版本,JVM供应商和SecurityManager
。
还没完,如果我们想让bean和map之间互转,BeanCopier
是满足不了的,不过,cglib
里面还有一个BeanMap
可以做这样的事儿,BeanMap
是Map
的实现,我们可以把它当做一个Map
使用。
所以,我们写个beanCopy
的封装,是这样的(代码没有任何多余的部分):
public final class BeanCopyUtil {
private BeanCopyUtil() {
}
private static ThreadLocal<ObjenesisStd> objenesisStdThreadLocal = ThreadLocal.withInitial(ObjenesisStd::new);
private static ConcurrentHashMap<Class<?>, ConcurrentHashMap<Class<?>, BeanCopier>> cache = new ConcurrentHashMap<>();
public static <T> T copy(Object source, Class<T> target) {
return copy(source, objenesisStdThreadLocal.get().newInstance(target));
}
public static <T> T copy(Object source, T target) {
BeanCopier beanCopier = getCacheBeanCopier(source.getClass(), target.getClass());
beanCopier.copy(source, target, null);
return target;
}
public static <T> List<T> copyList(List<?> sources, Class<T> target) {
if (sources.isEmpty()) {
return Collections.emptyList();
}
ArrayList<T> list = new ArrayList<>(sources.size());
ObjenesisStd objenesisStd = objenesisStdThreadLocal.get();
for (Object source : sources) {
if (source == null) {
throw new ApiException(SysCode.SYS2000);
}
T newInstance = objenesisStd.newInstance(target);
BeanCopier beanCopier = getCacheBeanCopier(source.getClass(), target);
beanCopier.copy(source, newInstance, null);
list.add(newInstance);
}
return list;
}
public static <T> T mapToBean(Map<?, ?> source, Class<T> target) {
T bean = objenesisStdThreadLocal.get().newInstance(target);
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(source);
return bean;
}
public static <T> Map<?, ?> beanToMap(T source) {
return BeanMap.create(source);
}
private static <S, T> BeanCopier getCacheBeanCopier(Class<S> source, Class<T> target) {
ConcurrentHashMap<Class<?>, BeanCopier> copierConcurrentHashMap = cache.computeIfAbsent(source, aClass -> new ConcurrentHashMap<>(16));
return copierConcurrentHashMap.computeIfAbsent(target, aClass -> BeanCopier.create(source, target, false));
}
}
上面只是一个参考,建议大家自己看下cglib的bean包源码,很有意思,生成字节码的部分在net.sf.cglib.beans.BeanCopier.Generator#generateClass
。
我上面说过BeanCopier
只支持符合javaBean规范的,原因在这里:
在generateClass
方法中,通过ReflectUtils.getBeanGetters
获取源类型和目标类型的getter方法
PropertyDescriptor[] getters = ReflectUtils.getBeanGetters(this.source);
PropertyDescriptor[] setters = ReflectUtils.getBeanGetters(this.target);
Map names = new HashMap();
for(int i = 0; i < getters.length; ++i) {
names.put(getters[i].getName(), getters[i]);
}
...
for(int i = 0; i < setters.length; ++i) {
...
}
而ReflectUtils
虽然看名字是反射工具类,但getBeanGetters
方法是通过反省来拿到对应方法的,
BeanInfo info = Introspector.getBeanInfo(...)
所以,它原则需要符合javaBean
规范的属性拷贝。
看到我这么说,是不是感觉有漏洞,再怎么反省,它拿的还是get set方法,只要源类有get方法,目标类有set方法,就没问题,属性名首字母大写也没问题。
最后一点,属性名相同,但是类型不同的,怎么办?
我们看源码:
if (useConverter) {
...
} else if (compatible(getter, setter)) {
e.dup2();
e.invoke(read);
e.invoke(write);
}
private static boolean compatible(PropertyDescriptor getter, PropertyDescriptor setter) {
// TODO: allow automatic widening conversions?
return setter.getPropertyType().isAssignableFrom(getter.getPropertyType());
}
只有在compatible
返回true的时候,才能拷贝值,不然就只能通过转换器Converter
了,这里说一下,如果使用了转换器,所有的属性都会走转换器,所以转换器里需要考虑所有类型的情况,这里就不做演示了。
总结
我个人非常喜欢用cglib里的包,cglib的BeanCopier效率也是非常高的,推荐使用。