1

开箱即用的 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;
    }
}
4

3 回答 3

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;
    }
于 2015-10-31T00:39:00.593 回答
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());
}
于 2009-03-05T13:59:45.593 回答
0
public class Destination {
    private boolean property;

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

    public void setProperty(boolean property) {
        this.property = property;
    }
}
于 2010-05-28T08:39:40.073 回答