如何使用BeanUtils copyProperties从布尔值复制到布尔值?

时间:2009-03-05 13:23:53

标签: java apache-commons-beanutils

开箱即用的BeanUtils copyProperties似乎不会处理从布尔对象属性到布尔基元属性的复制。

我想我可以创建并注册一个转换器来处理这个问题,但这似乎没有用。

那么,我如何使用BeanUtils将属性从类Source复制到类Destination:

public class Destination {

    private boolean property;

    public boolean isProperty() {
        return property;
    }

    public void setProperty(boolean property) {
        this.property = property;
    }
}


public class Source{

    private Boolean property;

    public Boolean getProperty() {
        return property;
    }

    public void setProperty(Boolean property) {
        this.property = property;
    }
}

3 个答案:

答案 0 :(得分:2)

try creating both 
/*by default beanutils copyproperties looks for below method if you use either apache or spring flavour of beanutils.
always prefer using apache 1.9.2 ( fixed many bugs) but bit slow compared with spring beanutils.*/
 public Boolean getProperty() {
        return property;
    }
//which is used by some frameworks 
 public Boolean isProperty() {
        return property;
    }

答案 1 :(得分:0)

实际上反之亦然:

public static void main(String[] args) throws Exception {
    Source d = new Source();
    d.setProperty(Boolean.TRUE);
    BeanMap beanMap = new BeanMap(d);

    Destination s = new Destination();
    BeanUtils.populate(s, beanMap);
    System.out.println("s.getProperty()=" + s.isProperty());
}

答案 2 :(得分:0)

public class Destination {
    private boolean property;

    // code getProperty() instead
    public boolean isProperty() {
        return property;
    }

    public void setProperty(boolean property) {
        this.property = property;
    }
}