有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何正确使用Apache Common BeanUtil的BeanComparator来实现内省?

我在软件上搜索了一下,但没有找到一个与BeanUtil使用相关的基本问题

我有一个POJO类,比如说UserPojo,它的类代码是:

public class UserPojo{

    private String name;
    private int    gender;
    private int    size;

    //Setters
    public void setName(String name) {this.name  =name;}
    public void setGender(int gender){this.gender=gender;}
    public void setSize(int size)    {this.size  =size;}

    //getters
    public String getName()  {return this.name;}
    public int    getGender(){return this.gender;}
    public int    getSize()  {return this.size;}
}

我的问题是,如何使用BeanUtil自动比较这个bean的两个实例

我试过这个:

final BeanComparator<UserPojo> comparator = new BeanComparator<UserPojo>();
final int comparison = comparator.compare(expectedPojo, receivedPojo);

但它以以下错误告终:

java.lang.ClassCastException : UserPojo cannot be cast to java.lang.Comparable

我知道我的Pojo应该实现标准的Comparable接口,但这样比较就不依赖于内省,BeanUtil的导入似乎非常无用

那么,如何正确使用它呢


共 (0) 个答案