您的位置:首页 > 编程语言 > Java开发

bean之间的复制!BeanUtils.copyProperties、set、BeanCopier还有spring中的BeanUtils.copyProperties之间的区别

2015-06-25 12:00 706 查看
  我们一般对进行web开发,在进行对form里的属性值跟实体类复制时,我们大概用到了几种方法,一般常见的set进行复制,
  struts自带的BeanUtils.copyProperties、spring差不多的BeanUtils.copyProperties、还有cglib架包中的BeanCopier,
  如果你使用set进行复制就会感觉到代码的冗长,开发起来不方面,而struts自带的BeanUtils.copyProperties很简洁,直接丢两个
  对象进行返copy,而spring自带的BeanUtils.copyProperties跟strits差不多,但是还是有区别,我们下面考虑到性能的时候就知道了,
  而cglib中的BeanCopier用起来也很简洁,创建一个对象,然后用这个对象的方法进行复制,现在我们讲他们之间的性能
 如果用set,那它是原始的老大,相当快,在大型的数据中进行copy性能是最好的,而struts中的beanUtils.copyproperties那就惨了,
 他由于造BeanCopier是消耗很多性能的, 在执行复杂操作的时候, 最好能现缓存 这个对象。 不然容易发生一样的性能问题,性能是相当的垃圾
 如果你自己的项目不是特别的大对数据量很少,可以使用,而spring中的BeanUtils.copyProperties效率比struts中的是很快的,快几倍,
 但是本人喜欢用cglib中的BeanCopier,他用起来也很简洁,性能跟set差不多,下面就是我测试出来的数据,嘎嘎,希望大家以后也用BeanCopier.

 BeanUtils.copyProperties性能测试

com.wf.form.UserForm uf=(com.wf.form.UserForm)form;
User u=new User();
int LOOP_NUMBER=100000;
Long startTime;
Long endTime;
System.out.println(uf.getNAME());

startTime=System.currentTimeMillis();
for(int i=0;i<LOOP_NUMBER;i++){
BeanUtils.copyProperties(u, uf);
}
endTime=System.currentTimeMillis();
Long BeanUtilstime=endTime-startTime;
//----------------------------------------------------------
//set性能测试
startTime=System.currentTimeMillis();
for(int i=0;i<LOOP_NUMBER;i++){
u.setNAME(uf.getNAME());
u.setPWD(uf.getPWD());
u.setTEL(uf.getTEL());
u.setDZ(uf.getDZ());
u.setDEP(uf.getDEP());
}
endTime=System.currentTimeMillis();
Long sgtime=endTime-startTime;

//----------------------------------------------------------------
//BeanCopier copy=BeanCopier.create性能测试
BeanCopier copy=BeanCopier.create(UserForm.class, User.class, false);
startTime=System.currentTimeMillis();
for(int i=0;i<LOOP_NUMBER;i++){
copy.copy(uf, u, null);
}
endTime=System.currentTimeMillis();
Long copiertime=endTime-startTime;

//----------------------------------------------------------------------
//spring:BeanUtils.copyProperties性能测试
startTime=System.currentTimeMillis();
for(int i=0;i<LOOP_NUMBER;i++){
org.springframework.beans.BeanUtils.copyProperties( uf,u);
}
endTime=System.currentTimeMillis();
Long springBeanUtilstime=endTime-startTime;
//-------------------------------------------------------------
startTime=System.currentTimeMillis();
for(int i=0;i<LOOP_NUMBER;i++){
PropertyUtils.copyProperties(u, uf);
}
endTime=System.currentTimeMillis();
Long PropertyUtilstime=endTime-startTime;

System.out.println(u.getNAME());

System.out.println("BeanUtilstime............................"+BeanUtilstime+"ms");
System.out.println("PropertyUtilstime............................"+PropertyUtilstime+"ms");
System.out.println("settime............................"+sgtime+"ms");
System.out.println("copiertime............................"+copiertime+"ms");
System.out.println("springBeanUtilstime............................"+springBeanUtilstime+"ms");


结果:

BeanUtilstime............................937ms
PropertyUtilstime............................3927ms
settime............................16ms
copiertime............................35ms
springBeanUtilstime............................913ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java