各大BeanUtils性能总结

 

目录

前言

推荐使用

测试

输出结果

参考文章的测试结果

注意事项


个人总结,仅供参考

转自这篇文章

前言

首先说下这个类的好处,very big,比如新建的功能,你必须去判断是不是有数值,然后一个个去set,浪费很多时间。判断的话使用@Valid即可。可以使用BeanUtils直接对属性进行复制,十分方便。

 

推荐使用

至于性能方面,建议使用Spring BeanUtils BeanUtils.copyProperties(复制对象,复制后的对象)

还有一个更快的是Cglib的BeanCopier

不要使用Apache BeanUtils!!!比较慢


测试

public static void main(String[] args) {
        QuestionnaireQuestionDto peopleGroupdto = new QuestionnaireQuestionDto();
        peopleGroupdto.setQuestionnaireId(123L);
        peopleGroupdto.setId(321L);
        peopleGroupdto.setTypeId(123L);
        peopleGroupdto.setQuestionTitle("r3r1");
        peopleGroupdto.setRequiredFlag(0);
        peopleGroupdto.setQuestionType(0);
        peopleGroupdto.setBaseFlag(0);
        peopleGroupdto.setMinOptionSelect(0);
        peopleGroupdto.setMaxOptionSelect(0);
        peopleGroupdto.setSortType(0);
        peopleGroupdto.setStyleType(0);
        peopleGroupdto.setSort(0);
        peopleGroupdto.setValidateType(0);
        peopleGroupdto.setMinVal(new BigDecimal("0"));
        peopleGroupdto.setMaxVal(new BigDecimal("0"));
        peopleGroupdto.setMinValMsg(new BigDecimal("0"));
        peopleGroupdto.setMaxValMsg(new BigDecimal("0"));
        peopleGroupdto.setValidateRepeat(0);
        peopleGroupdto.setOptionDtoList(Lists.newArrayList());
        peopleGroupdto.setQuestionFileUrl("12412e");
        peopleGroupdto.setQuestionFileType("2eqedq");
        peopleGroupdto.setNoticeTime(231);
        peopleGroupdto.setIsNeedToAnswer(false);
        peopleGroupdto.setInsertFlag(false);
        peopleGroupdto.setTList(Lists.newArrayList());
        peopleGroupdto.setOptionName("wasdasf");
        peopleGroupdto.setAnswerCount("12ew");
        QuestionnaireQuestion questionnaireQuestion = new QuestionnaireQuestion();
        long time=System.nanoTime();
        BeanUtils.copyProperties(peopleGroupdto, questionnaireQuestion);
        long time1=System.nanoTime();
        System.out.println(time1-time);
        long time2=System.nanoTime();
        BeanCopier beanCopier=BeanCopier.create(QuestionnaireQuestionDto.class,QuestionnaireQuestion.class,false);
        beanCopier.copy(peopleGroupdto, questionnaireQuestion,null);
        long time3=System.nanoTime();
        System.out.println(time3-time2);
    }

输出结果

799270500
78523800

纳秒级别,可以看到他们之间相差10倍,一个是0.79秒,一个是0.078秒

 

参考文章的测试结果

各大BeanUtils性能总结_第1张图片

 

注意事项

在复制的时候记得判空,如果有参数为空会报错。比如复制对象为空,或者复制之后的对象为空!!!

 

 

你可能感兴趣的:(各大BeanUtils性能总结)