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

发布于 2024-07-14 18:09:27 字数 620 浏览 5 评论 0原文

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

BeanUtils copyProperties, out of the box, doesn't seem to handle copying from Boolean object properties to boolean primitive properties.

I figured I could create and register a converter to handle this, but that just didn't seem to work.

So, how can I use BeanUtils to copy the properties from class Source to class Destination where:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

—━☆沉默づ 2024-07-21 18:09:27
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;
    }
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;
    }
满天都是小星星 2024-07-21 18:09:27

实际上反之亦然:

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

It is actually vice-versa:

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());
}
千紇 2024-07-21 18:09:27
public class Destination {
    private boolean property;

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

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

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

    public void setProperty(boolean property) {
        this.property = property;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文