Java Bean 屬性復(fù)制的幾種框架比較

Java開發(fā)都避免不了和各種Bean打交道,包括POJO、BO、VO、PO、DTO等,而Java的應(yīng)用非常講究分層的架構(gòu),因此就會(huì)存在對(duì)象在各個(gè)層次之間作為參數(shù)或者輸出傳遞的過(guò)程,這里轉(zhuǎn)換的工作往往非常繁瑣。

為此業(yè)界有很多開源的解決方案,列出一些常見(jiàn)的如下:

性能測(cè)試

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>

測(cè)試代碼:

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("武昌區(qū)");
        address.setDetail("歡樂(lè)大道28號(hào)");
        customer.setAddress(address);

        return customer;
    }
}

分別測(cè)試10萬(wàn)次拷貝,耗時(shí)如下:

表1

框架 耗時(shí)
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

小結(jié)

總體來(lái)說(shuō),cglib BeanCopier效率最高,spring-beans 次之,BeanUtils 效果不太理想。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,954評(píng)論 6 342
  • spring官方文檔:http://docs.spring.io/spring/docs/current/spri...
    牛馬風(fēng)情閱讀 1,726評(píng)論 0 3
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan閱讀 5,419評(píng)論 7 56
  • 當(dāng)我犯病最嚴(yán)重的時(shí)候,本著一種對(duì)自己與社會(huì)都不負(fù)責(zé)任的態(tài)度,踏上了那場(chǎng)說(shuō)走就走的旅行。 當(dāng)時(shí)我患了一種精神類疾病,...
    斷劍殘燭閱讀 226評(píng)論 0 1