Dozer
Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.
Please read the about page for detailed information on Dozer.
---------------------------------------------------
Dozer支持 Java Bean 之间数据的递归拷贝。特别是一些复杂的java bean .
Dozer 支持简单的、复杂的、双向的、显隐式的映射以及递归映射。当然也支持集合属性及子元素的映射。
一般的BeanUtils需要copy的源和目标对象的字段名字要一致,而Dozer可以支持名字不一样的mapping,而且可以多个字段映射一个段,一个字段映射多个字段。
帮助文档
http://dozer.sourceforge.net/dozer-user-guide.pdf
突然想测试一下:
Spring BeanUtils / Apache common BeanUtils /Dozer 性能测试
测试代码
package com.practise.dozer; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; import com.practise.dozer.beans.DestinationObject; import com.practise.dozer.beans.SourceObject; public class TestPrimitive { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException { final Mapper mapper = new DozerBeanMapper(); final int times = 1000000; new Thread(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); DestinationObject destObject = null; for (int j = 0; j < times; j++) { destObject = mapper.map(getSourceObe(), DestinationObject.class); } System.out.println("Mapper cost " + (System.currentTimeMillis() - start)); System.out.println(destObject); } }).start(); new Thread(new Runnable() { @Override public void run() { DestinationObject destObject2 = new DestinationObject(); long start2 = System.currentTimeMillis(); for (int j = 0; j < times; j++) { try { org.apache.commons.beanutils.BeanUtils.copyProperties( destObject2, getSourceObe()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } System.out.println("Apache Common BeanUtils cost " + (System.currentTimeMillis() - start2)); System.out.println(destObject2); } }).start(); new Thread(new Runnable() { @Override public void run() { DestinationObject destObject2 = new DestinationObject(); long start2 = System.currentTimeMillis(); for (int j = 0; j < times; j++) { org.springframework.beans.BeanUtils.copyProperties( getSourceObe(), destObject2); } System.out.println("Spring BeanUtils cost " + (System.currentTimeMillis() - start2)); System.out.println(destObject2); } }).start(); } private static SourceObject getSourceObe() { int i = 10; String str = "this is a 中"; final SourceObject sourceObject = new SourceObject(i, str); DestinationObject innerDestObject = new DestinationObject(); innerDestObject.setI(100); innerDestObject.setStr("innerDestObject"); sourceObject.setDest(innerDestObject); sourceObject.setArray(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); sourceObject.setListStr(Arrays.asList("1", "2", "1", "2", "1", "2", "1", "2")); return sourceObject; } }
结果:
Spring BeanUtils cost 1941
DestinationObject(i=10, str=this is a 中, dest=DestinationObject(i=100, str=innerDestObject, dest=null, array=null, listStr=null), array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], listStr=[1, 2, 1, 2, 1, 2, 1, 2])
Apache Common BeanUtils cost 17832
DestinationObject(i=10, str=this is a 中, dest=DestinationObject(i=100, str=innerDestObject, dest=null, array=null, listStr=null), array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], listStr=[1, 2, 1, 2, 1, 2, 1, 2])
Mapper cost 58599
DestinationObject(i=10, str=this is a 中, dest=DestinationObject(i=100, str=innerDestObject, dest=null, array=null, listStr=null), array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], listStr=[1, 2, 1, 2, 1, 2, 1, 2])