用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));
common属性包可实现如下动态bean,但是假的bean
最新推荐文章于 2025-07-13 10:22:51 发布
本文介绍如何使用Commons BeanUtils库中的BasicDynaClass和BasicDynaBean来动态创建Bean实例,并设置属性值。此外,还展示了通过反射获取Bean属性的方法,尽管在示例中遇到了NoSuchMethodException的问题。
536

被折叠的 条评论
为什么被折叠?



