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

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

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

性能测试

1、maven依赖

commons-beanutils

commons-beanutils

1.9.3

org.springframework

spring-beans

4.3.6.RELEASE

cglib

cglib

3.2.5

net.sf.dozer

dozer

5.5.1

测试代码:

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

org.springframework.beans.BeanUtils.copyProperties(src, target);

}

}

public void testApacheBeanUtils(Object src, Object dest) throws InvocationTargetException, IllegalAccessException {

for(int i=0; i

BeanUtils.copyProperties(dest, src);

}

}

public void testApachePropertyUtils(Object src, Object dest) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {

for(int i=0; i

PropertyUtils.copyProperties(dest, src);

}

}

private Customer getCustomer(){

Customer customer = new Customer();

customer.setId(15L);

customer.setName("ricky");

customer.setAge(28);

List 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 效果不太理想。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值