6

我正在尝试使用 PropertyUtils 类的 copyProperties 方法来复制 bean。

问题是,如果布尔值的 getter 写为“isXXX”,则它无法复制布尔值。仅当布尔值的 getter 为“getXXX”时才有效。例如,

class MyBean {
....
    public boolean isEnabled() {
        return enabled;
    }
....
}

PropertyUtils.copyProperties 不适用于此类。但它适用于此:

class MyBean {
....
    public boolean getEnabled() {
        return enabled;
    }
....
}

有没有什么办法解决这一问题?

非常感谢

4

1 回答 1

7

这取决于enabled类型:

如果是boolean,则 getter 必须采用以下形式:

public boolean isEnabled() {
    return enabled;
}

如果是Boolean,则 getter 必须采用以下形式:

// The return type of the function doesn't matter Boolean or boolean
public Boolean getEnabled() {
    return enabled;
}
于 2013-09-15T13:14:10.130 回答