Banq,請教如何動態實現標準java bean?

hgwnet發表於2005-07-28
用common屬性包可實現如下動態bean,但是假的bean:
DynaProperty[] px = {
new DynaProperty("firstName", Class.forName("java.lang.String")),
new DynaProperty("lastName", Class.forName("java.lang.String"))
};
DynaClass dynaClass = new BasicDynaClass("employee", null, px);

DynaBean employee = dynaClass.newInstance();
PropertyUtils.setProperty(employee, "firstName", "Fred");
PropertyUtils.setProperty(employee, "lastName", "Flintstone");

System.out.println("firstName:" + PropertyUtils.getProperty(employee, "firstName"));
System.out.println("lastName:" + PropertyUtils.getProperty(employee, "lastName"));

由於特殊需要,須按如下反射方式取得bean屬性,但系統丟擲NoSuchMethodException:

Method method = employee.getClass().getMethod("getLastName", null);
debug(method.invoke(employee, null));

可否告知有無方法實現一個標準規範的bean,即可按反射方式取得屬性。
先多謝了。

相關文章