[Java 工具类]测试BeanUtils.copyProperties

平时工作中很常见的一个场景是对象属性的复制,这时我们使用最多的是两种方式

  1. 调用引用包中的BeanUtils.copyProperties方法,调用很简单,只需要一行代码,还可以设置
  2. 自己写get、set方法逐个设置

目前常用的工具类是“org.springframework.beans.BeanUtils”或“org.apache.commons.beanutils.BeanUtils”,为知道那个方法效率高,我写了一个测试类,如下:

import org.junit.Before;
import org.junit.Test;
import org.omg.PortableServer.THREAD_POLICY_ID;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StopWatch;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TestW {

    OrderInfoDto source;
    int repeattime = 100000;

    @Before
    public void before() {
        source = new OrderInfoDto();
        source.setId(123L);
        source.setBundingTime(new Date());
        source.setBadJudgeState(14);
        source.setBuyerMobile("123456789");
        source.setBuyerName("测试");
        source.setTs(new Date());
        source.setMark(2);

        source.setOrderNo(12L);
        source.setCreateDate(new Date());
        source.setCreateTime(new Date());
        source.setBundingTime(new Date());
        source.setOrderCombineBeginTime(new Date());
        source.setUpdateTime(new Date());
        source.setPreFinishTime(new Date());
        source.setPickingBeginTime(new Date());
        source.setPickingEndTime(new Date());
        source.setOrderPreDeliveryTime(new Date());
    }

    @Test
    public void test0() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < repeattime ; i++) {
            OrderInfoDto target = new OrderInfoDto();
            target.setId(source.getId());
            target.setBundingTime(source.getBundingTime());
            target.setBadJudgeState(source.getBadJudgeState());
            target.setBuyerMobile(source.getBuyerMobile());
            target.setBuyerName(source.getBuyerName());
            target.setTs(source.getTs());
            target.setMark(source.getMark());

            target.setOrderNo(source.getOrderNo());
            target.setCreateDate(source.getCreateDate());
            target.setCreateTime(source.getCreateDate());
            target.setBundingTime(source.getCreateDate());
            target.setOrderCombineBeginTime(source.getCreateDate());
            target.setUpdateTime(source.getCreateDate());
            target.setPreFinishTime(source.getCreateDate());
            target.setPickingBeginTime(source.getCreateDate());
            target.setPickingEndTime(source.getCreateDate());
            target.setOrderPreDeliveryTime(source.getCreateDate());
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());
    }

    @Test
    public void testSpring() throws InterruptedException {
//        TimeUnit.SECONDS.sleep(3);

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < repeattime ; i++) {
            OrderInfoDto target = new OrderInfoDto();
            BeanUtils.copyProperties(source,target);
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());

//        TimeUnit.DAYS.sleep(1);
    }

    @Test
    public void testApache() throws InvocationTargetException, IllegalAccessException, InterruptedException {
//        TimeUnit.SECONDS.sleep(3);

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < repeattime ; i++) {
            OrderInfoDto target = new OrderInfoDto();
            org.apache.commons.beanutils.BeanUtils.copyProperties(target,source);
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());

//        TimeUnit.DAYS.sleep(1);

    }

}

结果如下,其中java方式是使用get/set方式设置属性值。

[Java 工具类]测试BeanUtils.copyProperties_第1张图片
image.png

从结果来看使用get/set方法是最快的。spring和Apache都是用了反射机制实现的属性复制。

那为什么反射会比直接使用get/set慢那么多呢!我从java 官方的 javatutorials 找到了一些线索。

Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.
Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.

建议: 对性能要求比较高的系统不要偷懒,使用get/set方式。

问题:在测试的时候想知道spring或Apache哪个操作花的时间比较多,但是一直没找到比较通用的方法,能得到某个方法的执行时间。有好心的网友能给出答案不胜感激额!

你可能感兴趣的:([Java 工具类]测试BeanUtils.copyProperties)