Copy specific fields by using BeanUtils.copyProperties?

前端 未结 5 1922
执笔经年
执笔经年 2020-12-12 14:13

springframework.beans.BeanUtils is very useful to copy objects, and I use the \"ignoreProperties\" option frequently. However, sometimes I want to copy only spe

5条回答
  •  清歌不尽
    2020-12-12 14:40

    Here is an Example with Spring BeanUtils class:

    public static void copyList(List sourceList,
            List targetList, Class targetType) {
    
        try {
    
            for (Object source : sourceList) {
                Object target = null;
                target = targetType.newInstance();
                BeanUtils.copyProperties(source, target);
                targetList.add(target);
            }
    
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题