使用BeanUtils.copyProperties复制特定字段?

时间:2011-02-22 14:35:00

标签: java spring spring-mvc apache-commons

springframework.beans.BeanUtils对于复制对象非常有用,我经常使用“ignoreProperties”选项。但是,有时我只想复制特定对象(基本上,与“忽略属性”相反)。有谁知道我该怎么做?任何帮助将不胜感激。

import org.springframework.beans.BeanUtils;

public class Sample {    
    public static void main(String[] args) {    
        DemoADto demoADto = new DemoADto();
        demoADto.setName("Name of Demo A");
        demoADto.setAddress("Address of Demo A");

        DemoBDto demoBDto = new DemoBDto();

        // This is "ignoreProperties" option
        // But I want to know how I can copy only name field by using BeanUtils or something.
        BeanUtils.copyProperties(demoADto, demoBDto, new String[] {"address"});

        System.out.println(demoBDto.getName());
        System.out.println(demoBDto.getAddress());    
    }    
}

public class DemoADto {    
    private String name;    
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }    
}

public class DemoBDto {    
    private String name;    
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }    
}

5 个答案:

答案 0 :(得分:57)

您可以使用BeanWrapper技术。这是一个示例实现:

public static void copyProperties(Object src, Object trg, Iterable<String> props) {

    BeanWrapper srcWrap = PropertyAccessorFactory.forBeanPropertyAccess(src);
    BeanWrapper trgWrap = PropertyAccessorFactory.forBeanPropertyAccess(trg);

    props.forEach(p -> trgWrap.setPropertyValue(p, srcWrap.getPropertyValue(p)));

}

或者,如果你真的,真的想要使用BeanUtils,这是一个解决方案。反转逻辑,通过将完整属性列表与包含:

进行比较来收集排除
public static void copyProperties2(Object src, Object trg, Set<String> props) {
    String[] excludedProperties = 
            Arrays.stream(BeanUtils.getPropertyDescriptors(src.getClass()))
                  .map(PropertyDescriptor::getName)
                  .filter(name -> !props.contains(name))
                  .toArray(String[]::new);

    BeanUtils.copyProperties(src, trg, excludedProperties);
}

答案 1 :(得分:3)

如果您不想使用Commons BeanUtils,您仍然可以使用BeanWrapper来使用Spring。

您必须手动遍历所有属性,因此您需要创建一个静态辅助方法。

您可以随时复制copyProperties正在执行的操作并根据您的喜好进行调整: http://tinyurl.com/BeanUtils-copyProperties

答案 2 :(得分:2)

检查出来:BeanPropertyCopyUtil

示例:

copyProperties(user, systemUser, "first firstName", "last lastName");

您还需要Apache Commons BeanUtils。

答案 3 :(得分:0)

以下是Spring BeanUtils类的示例:

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();
    }
}

答案 4 :(得分:0)

你可以使用     org.springframework.beans.BeanUtils.copyProperties(Object source,Object target,Class editable)抛出BeansException

确保目标实现可编辑的接口,该接口定义了将要复制的属性。