我怎样才能处理像豆子一样的POJO?

tan*_*ens 2 java reflection javabeans

如何作为bean访问一个简单的java对象?

例如:

class Simple {
    private String foo;
    String getFoo() {
        return foo;
    }
    private void setFoo( String foo ) {
        this.foo = foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想像这样使用这个对象:

Simple simple = new Simple();
simple.setFoo( "hello" );

checkSettings( simple );
Run Code Online (Sandbox Code Playgroud)

所以我正在寻找该方法的实现checkSettings( Object obj ):

public boolean checkSettings( Object obj ) {
    // pseudocode here
    Bean bean = new Bean( obj );
    if( "hello".equals( bean.getAttribute( "foo" ) ) {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

java语言包含一个叫做java.beans听起来可以帮助我的软件包.但我找不到一个好的起点.

任何提示?

Nic*_*kDK 6

我认为您正在寻找的功能类似于BeanAtils类的apache-commons:

http://commons.apache.org/beanutils/

看一下BeanUtils的getProperty()方法.