几种Bean的复制方法性能比较
由于项目对性能 速度要求很高,表中的字段也很多,存在一个复制方法,耗时相对比较长,经过测试,使用Apache,Spring等提供的方法 耗时较长,使用自己自定义的复制方法时间提升很多,现记录下。
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xsjt.compare</groupId>
<artifactId>copy-property</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies>
<!-- apache -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency> <!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency> <!-- cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.3</version>
</dependency> <!-- ezmorph -->
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency> </dependencies>
</project>
2.定义一个实体bean
package com.xsjt.bean; import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date; /**
* 用户实体类
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class User implements Serializable{ private int id;
private String userName;
private String userPass;
private String phone;
private Date birth;
private BigDecimal totleMoney;
private String remark; public User() {
super();
} public User(int id, String userName, String userPass, String phone, Date birth, BigDecimal totleMoney, String remark) {
super();
this.id = id;
this.userName = userName;
this.userPass = userPass;
this.phone = phone;
this.birth = birth;
this.totleMoney = totleMoney;
this.remark = remark;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public BigDecimal getTotleMoney() {
return totleMoney;
}
public void setTotleMoney(BigDecimal totleMoney) {
this.totleMoney = totleMoney;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
} @Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPass=" + userPass + ", phone=" + phone + ", birth=" + birth + ", totleMoney=" + totleMoney + ", remark=" + remark + "]";
} }
3.自己定义的bean复制方法
package com.xsjt.util; import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import com.xsjt.bean.User; /**
* ClassName:CopyBeanProperty
* 在对象之间 进行属性值 复制
* Date: 2017年7月19日 下午5:53:07
* @author Joe
* @version
* @since JDK 1.8
*/
public class CopyBeanProperty { /**
* 将 旧的bean的值 赋值 给 新的bean
* @param oldObj
* @param newObj
*/
public static void copyproperty(Object oldObj ,Object newObj){ Class<?> clz = oldObj.getClass();
Field[] cols = clz.getDeclaredFields(); String name = null;
Object value = null;
try {
for(Field col : cols){
if(!col.isAccessible()){
// 设置可访问
col.setAccessible(true);
}
name = col.getName();
value = col.get(oldObj);
// 赋值给 新的bean
setBeanProperty(name ,value ,newObj);
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 将 name 和 value 赋值给 新的bean
* @param name
* @param value
* @param newObj
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private static void setBeanProperty(String name, Object value, Object newObj) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Class<?> clz = newObj.getClass();
Field col = clz.getDeclaredField(name);
if(!col.isAccessible()){
// 设置可访问
col.setAccessible(true);
}
col.set(newObj, value);
} /**
* 循环输入 属性 和 值 ,测试的 时候 使用
* @param newObj
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private static void loopBeanProperty(Object newObj) throws IllegalArgumentException, IllegalAccessException{
Class<?> clz = newObj.getClass();
Field[] cols = clz.getDeclaredFields();
for(Field col : cols){
if(!col.isAccessible()){
// 设置可访问
col.setAccessible(true);
}
System.out.println(col.getName() + "-->" + col.get(newObj));
}
} /**
* 测试
* @param args
*/
public static void main(String[] args) {
User user = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
try {
loopBeanProperty(user);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
4.各种bean复制方法的比较
package com.xsjt.compare; import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import com.xsjt.bean.User;
import com.xsjt.util.CopyBeanProperty;
import net.sf.cglib.beans.BeanCopier;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher; /**
* ClassName:CompareTest
* 测试 各种 复制 方法的性能
* Date: 2017年7月19日 下午5:53:07
* @author Joe
* @version
* @since JDK 1.8
*/
public class CompareTest { public static void main(String[] args) throws Exception { // 1.使用 apache 的 复制 方法
User orig = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
User dest = new User();
long start = System.currentTimeMillis();
BeanUtils.copyProperties(dest, orig);
System.out.println("1.apache的方法 花费的时间:" + (System.currentTimeMillis() - start) + "ms");
System.out.println("dest==" + dest); System.out.println("--------------------------------我是分割线------------------------------------------------"); // 2.使用 spring 的 复制方法
User source = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
User target = new User();
start = System.currentTimeMillis();
org.springframework.beans.BeanUtils.copyProperties(source, target);
System.out.println("2.spring的方法 花费的时间:" + (System.currentTimeMillis() - start) + "ms");
System.out.println("target==" + target); System.out.println("--------------------------------我是分割线------------------------------------------------"); // 3.使用 cglib 的复制方法
User source2 = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
User target2 = new User();
start = System.currentTimeMillis();
BeanCopier.create(source2.getClass(), target2.getClass(), false).copy(source2, target2, null);
System.out.println("3.cglib的方法 花费的时间:" + (System.currentTimeMillis() - start) + "ms");
System.out.println("target2==" + target2); System.out.println("--------------------------------我是分割线------------------------------------------------"); // 4.使用 EZMorph 的复制方法
User source3 = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
User target3 = new User();
start = System.currentTimeMillis();
MorpherRegistry registry = new MorpherRegistry();
registry.registerMorpher(new BeanMorpher(User.class, registry));
target3 = (User) registry.morph(User.class, source3);
System.out.println("4.EZMorph的方法 花费的时间:" + (System.currentTimeMillis() - start) + "ms");
System.out.println("target3==" + target3); System.out.println("--------------------------------我是分割线------------------------------------------------"); // 5.使用 自定义 的复制方法
User oldUser = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
User newUser = new User();
start = System.currentTimeMillis();
CopyBeanProperty.copyproperty(oldUser, newUser);
System.out.println("5.自定义的方法 花费的时间:" + (System.currentTimeMillis() - start) + "ms");
System.out.println("newUser==" + newUser);
}
}
5.运行结果展示

自定义的复制方法,使用了Java的反射机制实现,虽然其他的方法也是通过反射实现。但是自己定义的耗时时间最少,记录下,以备后用。
6.源码下载
https://git.oschina.net/xbq168/copy-property.git
几种Bean的复制方法性能比较的更多相关文章
- JavaBean ,Enterprise Bean(EJB), 三种Bean, 以及POJO
Bean简单的理解,可以理解为组件,一组通用方法的组合: JavaBean就可以称为Java组件,是所有组件的统称,EJB称为企业级 Java组件: 三种Bean: 1). session beans ...
- 几种流行Webservice框架性能对照
转自[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1 摘要 开发webservice应用程序中 ...
- Spring学习(二):Spring支持的5种Bean Scope
序言 Scope是定义Spring如何创建bean的实例的.Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope ...
- 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程
一.依赖注入DI 学习目标1)搞清楚构造参数依赖注入的过程及类2)搞清楚注解方式的属性依赖注入在哪里完成的.学习思路1)思考我们手写时是如何做的2)读 spring 源码对比看它的实现3)Spring ...
- 固态硬盘和机械硬盘的比较和SQLSERVER在两种硬盘上的性能差异
固态硬盘和机械硬盘的比较和SQLSERVER在两种硬盘上的性能差异 在看这篇文章之前可以先看一下下面的文章: SSD小白用户收货!SSD的误区如何解决 这样配会损失性能?实测6种特殊装机方式 听说固态 ...
- Go_18: Golang 中三种读取文件发放性能对比
Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...
- Golang 中三种读取文件发放性能对比
Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...
- 无状态会话Bean、有状态会话Bean、CMP与BMP中,哪一种Bean不需要自己书写连接数据库的代码?
无状态会话Bean.有状态会话Bean.CMP与BMP中,哪一种Bean不需要自己书写连接数据库的代码? A.无状态会话Bean B.有状态会话Bean C.CMP D.BMP 解答:C
- EJB包含哪3种bean
EJB包含哪3种bean 解答:session bean(会话bean), entity bean(实体bean), message bean(消息bean)
随机推荐
- 【转】常用 Microsoft .NET Framework 各版本下載網址列表
研究] 常用 Microsoft .NET Framework 各版本下載網址列表 2014-05-23 僅列常用的 (IA64, Beta, hotfix, ... 不列) Microsoft .N ...
- 自然语言交流系统 phxnet团队 创新实训 项目博客 (十)
关于本项目中使用到的庖丁分词的总结: Paoding 详细介绍 庖丁中文分词库是一个使用Java开发的,可结合到Lucene应用中的,为互联网.企业内部网使用的中文搜索引擎分词组件.Paodi ...
- js dom 创建table标签和子属性, 以及创建多选框
代码: <div class="Category"> <span id="Edit_headerTitle">Edit Categori ...
- SAP HR模块的基础数据表和增强配置
信息类型是SAP HR模块数据单元,用于对人员数据的记录和维护,是HR的基础.信息类型按照其创建方式的不同可以分为:人事信息类型.组织信息类型.信息类型数据的维护主要在事物码PA30.PA40.po1 ...
- android O 蓝牙设备默认名称更改
安卓系统会首先读取BTM_DEF_LOCAL_NAME的值,如果为空,就使用"ro.product.model"作为蓝牙设备名. system/bt/btif/src/btif_d ...
- Apache Commons CLI
简单的说,就是对命令的参数进行定义和解析的工具 -- 这里说的参数是我们常用的说法,而CLI里则是Option.Options,参数值(如果有)则是Option的arg(s). ## 为什么 那么,为 ...
- Java如何暂停线程一段时间?
在Java编程中,如何暂停线程一段时间? 以下示例显示如何通过创建sleepThread()方法来暂停线程一段时间. package com.yiibai; public class Suspendi ...
- svn -- 数据备份,版本回退,版本冲突,多仓库配置
数据备份 差异存储法: 版本回退 版本冲突 原理图: 解决办法: 三种方案: 1)合理分配项目开发模块 wangcai:文章,邮件,会员 xiaoqiang:静态化,缓存,前台 2)合理分配项目开发时 ...
- Android中的缩略图加载-不浪费一点多余的内存
1. Why,为什么要加载缩略图? 有的时候不需要展示原图,只需展示图片的缩略图,可以节省内存.比如:网易新闻中的图片浏览,左边展示的小狮子图片就是一个缩略图,点击这个图片,才会展示原图. 2. ...
- zookeeper和dubbo的关系[转]
Dubbo建议使用Zookeeper作为服务的注册中心. 1. Zookeeper的作用: zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...