性能优化 ---- 避免使用 BeanUtils copy

本文探讨了在高流量系统中,BeanUtils.copy 方法的性能问题。通过与Apache、Spring、CGLib及直接使用Getter/Setter的性能对比,得出Getter/Setter方式在性能上优于BeanUtils.copy。尽管BeanUtils.copy提供了简洁的代码,但其基于反射的实现导致性能下降,且在查找属性设置来源时存在困难。
摘要由CSDN通过智能技术生成

一、背景

在开发过程中,我们经常会遇到对象的转换,比如外部的 DTO 对象转换为内部的 DO 对象,这里面很多字段名都是相等的,要是一个一个去 get/set 很多人会觉得很烦,于是为了方便和代码的简介大家不约而同地找到了 UtilsBean.copy 相关的对象属性copy工具包。

当我们系统还只是 QPS 几十上百时,可能我们对系统的性能优化还不用到这么细致,当我们的系统 QPS/TPS 达到几十万上百万的时候,UtilsBean.copy 的开销问题就会凸显。

 

二、Apache、Spring、CGLib、Getter/Setter 属性copy 性能对比

package test.bean.copy;



import net.sf.cglib.beans.BeanCopier;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BeanCopyTest {


    public static void main(String[] args) throws Exception{
        TargetDTO targetDTO = new TargetDTO();
        SourceDTO sourceDTO = new SourceDTO();
        sourceDTO.setAge(18);
        sourceDTO.setName("wenniuwuren");
        int totalTimes = 10000;


        /**
         * Apache BeanUtils
         */
        long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < totalTimes; i++) {
            org.apache.commons.beanutils.BeanUtils.copyProperties(targetDTO, sourceDTO);
        }
        System.out.println("Apache BeanUtils single thread cost:" + (System.currentTimeMillis() - startTime1) + targetDTO.getName() + targetDTO.getAge());

        ExecutorService executorService4 = Executors.n
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值