动态创建bean
使用DynaBean,可以在程序运行时运态创建一个bean.(像struts中的ActionFrom有动态创建bean这个功能等)这个创建的bean也能被Commons BeanUtils中的类操作.下面的代码演示这个特性
// 动态创建bean中含有的属性,这个例子中有name和age这两个属性
转自:http://www.blogjava.net/hexuzhong/archive/2005/12/01/22141.html
使用DynaBean,可以在程序运行时运态创建一个bean.(像struts中的ActionFrom有动态创建bean这个功能等)这个创建的bean也能被Commons BeanUtils中的类操作.下面的代码演示这个特性
// 动态创建bean中含有的属性,这个例子中有name和age这两个属性
DynaProperty[] beanProperties = new DynaProperty[] {
new DynaProperty("name", String.class),
new DynaProperty("age", Integer.class),
};
BasicDynaClass personClass = new BasicDynaClass("person", BasicDynaBean.class, beanProperties);
// 生成这个bean的一个实例
DynaBean person = personClass.newInstance();
// 设置name属性,age属性的值
person.set("name", "hxz");
person.set("age", new Integer(21));
// 输出
// hxz
// 21
System.out.println(person.get("name"));
System.out.println(person.get("age"));
// 输出 21
// Commons BeanUtils包中的类也能操作这个动态生成的bean
System.out.println(PropertyUtils.getSimpleProperty(person, "name"));
转自:http://www.blogjava.net/hexuzhong/archive/2005/12/01/22141.html