Bean 复制的几种框架比较

4 种复制方法:

  • Apache BeanUtils
  • Apache PropertyUtils
  • Spring BeanUtils
  • Cglib BeanCopier

测试 Bean,FromBean 与 ToBean 属性一致

public class FromBean {
    private String name;
    private int age;
    private double money;

    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

测试方法

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.cglib.beans.BeanCopier;

public class Test {

    public static void main(String[] args) {
        FromBean fb = new FromBean();
        fb.setAge(20);
        fb.setMoney(30000.111);
        fb.setName("测试");

        int count = 10;

        convert((tb) -> BeanUtils.copyProperties(tb, fb), count, "Apache BeanUtils.copyProperties");

        convert((tb) -> PropertyUtils.copyProperties(tb, fb), count, "Apache PropertyUtils.copyProperties");

        convert((tb) -> org.springframework.beans.BeanUtils.copyProperties(fb, tb), count, "Spring BeanUtils.copyProperties");

        BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class, false);

        convert((tb) -> bc.copy(fb, tb, null), count, "Cglib BeanCopier.copy");

    }

    public static void convert(ConvertBean cb, int count, String type) {
        try {
            System.out.println(type + "开始进行测试");
            long start = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                ToBean tobean = new ToBean();
                cb.convertMethod(tobean);
                System.out.println(tobean.getAge());
                System.out.println(tobean.getMoney());
                System.out.println(tobean.getName());
            }
            System.out.println("耗时 " + (System.currentTimeMillis() - start) + " ms");
            System.out.println("----------------------");
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

@FunctionalInterface
interface ConvertBean {

    void convertMethod(ToBean tb) throws Exception;
}

打印结果

Apache BeanUtils.copyProperties开始进行测试
耗时 1178 ms
----------------------
Apache PropertyUtils.copyProperties开始进行测试
耗时 5 ms
----------------------
Spring BeanUtils.copyProperties开始进行测试
耗时 172 ms
----------------------
Cglib BeanCopier.copy开始进行测试
耗时 0 ms
----------------------

测试结果

单位:ms

count:10

次数:10第一次第二次第三次第四次平均值单次平均值
Apache BeanUtils1178102991710191035.75103.575
Apache PropertyUtils54845.250.525
Spring BeanUtils172257123135171.7517.175
Cglib BeanCopier000000

count:100

次数:100第一次第二次第三次第四次平均值单次平均值
Apache BeanUtils18681258162412601502.515.025
Apache PropertyUtils1610181715.250.1525
Spring BeanUtils193152209215192.251.9225
Cglib BeanCopier000000

count:10000

次数:10000第一次第二次第三次第四次平均值单次平均值
Apache BeanUtils16111657178615721656.50.16565
Apache PropertyUtils243641941111530.0153
Spring BeanUtils5512174334314080.0408
Cglib BeanCopier106376.50.00065

结论

  • 数量越大平均每次耗时越少
  • 综合 Cglib BeanCopier 性能最好,但在使用前,要先用 BeanCopier.create 定义

Cglib在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值