Java Bean 属性复制的几种框架比较

Java开发都避免不了和各种Bean打交道,包括POJO、BO、VO、PO、DTO等,而Java的应用非常讲究分层的架构,因此就会存在对象在各个层次之间作为参数或者输出传递的过程,这里转换的工作往往非常繁琐。

为此业界有很多开源的解决方案,列出一些常见的如下:

性能测试

1、maven依赖

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.5</version>
</dependency>

<dependency>
    <groupId>net.sf.dozer</groupId>
    <artifactId>dozer</artifactId>
    <version>5.5.1</version>
</dependency>

测试代码:

package com.bytebeats.bean.copy;

import com.bytebeats.bean.copy.model.Address;
import com.bytebeats.bean.copy.model.Customer;
import com.bytebeats.bean.copy.model.Employee;
import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class BeanCopyDemo {

    private int count = 100000;

    public static void main( String[] args ) throws Exception {

        new BeanCopyDemo().testTime("ApacheBeanUtils");
    }

    public void testTime(String name) throws Exception {
        Customer customer = getCustomer();
        Employee employee = new Employee();

        long start = System.currentTimeMillis();
        switch (name){
            case "ApacheBeanUtils":
                testApacheBeanUtils(customer, employee);
                break;
            case "ApachePropertyUtils":
                testApachePropertyUtils(customer, employee);
                break;
            case "SpringBeanUtils":
                testSpringBeanUtils(customer, employee);
                break;
            case "CglibBeanCopier":
                testCglibBeanCopier(customer, employee);
                break;
        }

        System.out.println(name+" count: "+count+" cost: "+(System.currentTimeMillis() - start)+" ms");
    }

    public void testCglibBeanCopier(Object src, Object target){

        BeanCopier copier = BeanCopier.create(src.getClass(), target.getClass(), false);
        copier.copy(src, target, null);
    }

    public void testSpringBeanUtils(Object src, Object target){
        for(int i=0; i<count; i++){
            org.springframework.beans.BeanUtils.copyProperties(src, target);
        }
    }

    public void testApacheBeanUtils(Object src, Object dest) throws InvocationTargetException, IllegalAccessException {
        for(int i=0; i<count; i++){
            BeanUtils.copyProperties(dest, src);
        }
    }

    public void testApachePropertyUtils(Object src, Object dest) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        for(int i=0; i<count; i++){
            PropertyUtils.copyProperties(dest, src);
        }
    }

    private Customer getCustomer(){

        Customer customer = new Customer();
        customer.setId(15L);
        customer.setName("ricky");
        customer.setAge(28);

        List<String> hobbies = new ArrayList<>(4);
        hobbies.add("汽车");
        hobbies.add("旅游");
        hobbies.add("体育");
        hobbies.add("NBA");
        customer.setHobbies(hobbies);

        Address address = new Address();
        address.setProvince("湖北省");
        address.setCity("武汉市");
        address.setDistrict("武昌区");
        address.setDetail("欢乐大道28号");
        customer.setAddress(address);

        return customer;
    }
}

分别测试10万次拷贝,耗时如下:

表1

框架 耗时
org.apache.commons.beanutils.BeanUtils.copyProperties 1804 ms
org.apache.commons.beanutils.PropertyUtils.copyProperties 1171 ms
org.springframework.beans.BeanUtils.copyProperties 770 ms
net.sf.cglib.beans.BeanCopier.copy 147 ms

小结

总体来说,cglib BeanCopier效率最高,spring-beans 次之,BeanUtils 效果不太理想。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,810评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,118评论 6 342
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 5,716评论 0 3
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan阅读 10,762评论 7 56
  • 当我犯病最严重的时候,本着一种对自己与社会都不负责任的态度,踏上了那场说走就走的旅行。 当时我患了一种精神类疾病,...
    断剑残烛阅读 1,657评论 0 1

友情链接更多精彩内容