这篇文章主要讲解了“JAVA中Beans与Utils哪个更好”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JAVA中Beans与Utils哪个更好”吧!
1.apache BeanUtils 性能太差 日志太多
2.Apache PropertyUtils 性能可以参考,但是数据量大了比起Cglib 还是慢很多
3.Spring BeanUtils 上了一定的数量级别比较慢
4.Cglib BeanCopier 目前看来比较稳定
5.com.rits.cloning ,数量级别大了 也慢
package com.helloxin.restful.api; import com.helloxin.restful.api.bo.CarBO; import com.rits.cloning.Cloner; import lombok.extern.slf4j.Slf4j; import net.sf.cglib.beans.BeanCopier; import org.apache.commons.beanutils.PropertyUtils; import org.junit.Test; import org.springframework.beans.BeanUtils; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @Slf4j public class BeanCopyTest { final static public Integer NUM = 10000; @Test public void beanSpringCopyTest() { List<CarBO> list = createData(NUM); long start = System.currentTimeMillis(); for(int i = 0;i< NUM;i++){ CarBO newCar = new CarBO(); BeanUtils.copyProperties(list.get(i), newCar); } log.info("duration={}", System.currentTimeMillis() - start); } @Test public void beanCglibCopyTest() { List<CarBO> list = createData(NUM); long start = System.currentTimeMillis(); BeanCopier b = BeanCopier.create(CarBO.class, CarBO.class, false); for(int i = 0;i< NUM;i++){ CarBO newCar = new CarBO(); b.copy(list.get(i), newCar, null); } log.info("duration={}", System.currentTimeMillis() - start); } @Test public void beanApacheCopyTest() throws InvocationTargetException, IllegalAccessException { List<CarBO> list = createData(NUM); long start = System.currentTimeMillis(); BeanCopier b = BeanCopier.create(CarBO.class, CarBO.class, false); for(int i = 0;i< NUM;i++){ CarBO newCar = new CarBO(); org.apache.commons.beanutils.BeanUtils.copyProperties(newCar,list.get(i)); } log.info("duration={}", System.currentTimeMillis() - start); } @Test public void beanApachePropertyUtilsCopyTest() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { List<CarBO> list = createData(NUM); long start = System.currentTimeMillis(); BeanCopier b = BeanCopier.create(CarBO.class, CarBO.class, false); for(int i = 0;i< NUM;i++){ CarBO newCar = new CarBO(); PropertyUtils.copyProperties(newCar,list.get(i)); } log.info("duration={}", System.currentTimeMillis() - start); } @Test public void beanRitsCloningTest() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { List<CarBO> list = createData(NUM); long start = System.currentTimeMillis(); Cloner cloner = new Cloner(); for(int i = 0;i< NUM;i++){ cloner.deepClone(list.get(i)); } log.info("duration={}", System.currentTimeMillis() - start); } private CarBO createData() { return CarBO.builder().make("st").numberOfSeats(12).build(); } private List<CarBO> createData(int num) { return IntStream.range(0, num).mapToObj(i -> CarBO.builder().make("st").numberOfSeats(i).build()).collect(Collectors.toList()); } }
感谢各位的阅读,以上就是“JAVA中Beans与Utils哪个更好”的内容了,经过本文的学习后,相信大家对JAVA中Beans与Utils哪个更好这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。