性能篇之对象拷贝工具BeanUtils.copyProperties和BeanCopier.copy的比较

对象的拷贝在开发过程中肯定非常常见,想必大家使用spring中的BeanUtils.copyProperties来完成的,小编最初也是用习惯了这个工具,但是在一次codereview中,大佬给我提出建议使用cglib的BeanCopier.copy方法来进行处理,也和我解释了前者性能比较差。所以我后来也去看了一下相关的源码。

BeanUtils.copyProperties:

 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<String> 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 value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }

                            writeMethod.invoke(target, value);
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }

BeanCopier.copy:

        public BeanCopier create() {
            Object key = BeanCopier.KEY_FACTORY.newInstance(this.source.getName(), this.target.getName(), this.useConverter);
            return (BeanCopier)super.create(key);
        }

        public void generateClass(ClassVisitor v) {
            Type sourceType = Type.getType(this.source);
            Type targetType = Type.getType(this.target);
            ClassEmitter ce = new ClassEmitter(v);
            ce.begin_class(46, 1, this.getClassName(), BeanCopier.BEAN_COPIER, (Type[])null, "<generated>");
            EmitUtils.null_constructor(ce);
            CodeEmitter e = ce.begin_method(1, BeanCopier.COPY, (Type[])null);
            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]);
            }

            Local targetLocal = e.make_local();
            Local sourceLocal = e.make_local();
            if (this.useConverter) {
                e.load_arg(1);
                e.checkcast(targetType);
                e.store_local(targetLocal);
                e.load_arg(0);
                e.checkcast(sourceType);
                e.store_local(sourceLocal);
            } else {
                e.load_arg(1);
                e.checkcast(targetType);
                e.load_arg(0);
                e.checkcast(sourceType);
            }

            for(int i = 0; i < setters.length; ++i) {
                PropertyDescriptor setter = setters[i];
                PropertyDescriptor getter = (PropertyDescriptor)names.get(setter.getName());
                if (getter != null) {
                    MethodInfo read = ReflectUtils.getMethodInfo(getter.getReadMethod());
                    MethodInfo write = ReflectUtils.getMethodInfo(setter.getWriteMethod());
                    if (this.useConverter) {
                        Type setterType = write.getSignature().getArgumentTypes()[0];
                        e.load_local(targetLocal);
                        e.load_arg(2);
                        e.load_local(sourceLocal);
                        e.invoke(read);
                        e.box(read.getSignature().getReturnType());
                        EmitUtils.load_class(e, setterType);
                        e.push(write.getSignature().getName());
                        e.invoke_interface(BeanCopier.CONVERTER, BeanCopier.CONVERT);
                        e.unbox_or_zero(setterType);
                        e.invoke(write);
                    } else if (compatible(getter, setter)) {
                        e.dup2();
                        e.invoke(read);
                        e.invoke(write);
                    }
                }
            }

            e.return_value();
            e.end_method();
            ce.end_class();
        }

可以看到前者是使用反射进行copy的对象,后者通过字节码技术生成一个代理类,直接调用这个类的set,get方法进行赋值。

而反射之所以慢是因为我们字节码文件在编译过程中会用到 即时编译器(JIT 编译器,Just In Time Compiler,这个编译器再将我们代码交给jvm之前会进行优化一遍,而反射需要动态解析,编译器无法优化,就会直接调用jvm的方法,这样效率就比较低下。所以从性能考虑建议使用BeanCopier.copy,当然也有其他的工具类效率更高的,但是各有各的优缺点,怎么选择看实际业务场景需要啦

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知BeanUtils.copyProperties()方法存在于两个不同的类中,分别为org.springframework.beans.BeanUtils和org.apache.commons.beanutils.BeanUtils。这两个类的copyProperties()方法传递参数的赋值是相反的,即在org.springframework.beans.BeanUtils中,copyProperties(A,B)的结果是将A拷贝到B;而在org.apache.commons.beanutils.BeanUtils中,copyProperties(A,B)的结果是将B拷贝到A。 因此,如果需要使用BeanUtils.copyProperties()方法,需要先确定使用的是哪个类中的方法,并根据需要传递正确的参数。 下面是两个类中copyProperties()方法的使用示例: 1. org.springframework.beans.BeanUtils中的copyProperties()方法,将A对象的属性值拷贝到B对象中: ```python from myapp.models import A, B from django.shortcuts import get_object_or_404 from django.http import HttpResponse from org.springframework.beans import BeanUtils def my_view(request, pk): a = get_object_or_404(A, pk=pk) b = B() BeanUtils.copyProperties(a, b) b.save() return HttpResponse('Copy successful!') ``` 2. org.apache.commons.beanutils.BeanUtils中的copyProperties()方法,将B对象的属性值拷贝到A对象中: ```python from myapp.models import A, B from django.shortcuts import get_object_or_404 from django.http import HttpResponse from org.apache.commons.beanutils import BeanUtils def my_view(request, pk): a = A() b = get_object_or_404(B, pk=pk) BeanUtils.copyProperties(a, b) a.save() return HttpResponse('Copy successful!') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值