Apache Commons BeanUtils包学习
<p><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">一、简介:</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> <a href="/misc/goto?guid=4959500937909635109" target="_blank">BeanUtils</a></span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">提供对</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> Java</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">反射和自省</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">API</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的包装。其主要目的是利用反射机制对</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">JavaBean</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的属性进行处理。我们知道,一个</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">JavaBean</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">通常包含了大量的属性,很多情况下,对</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">JavaBean</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的处理导致大量</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">get/set</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">代码堆积,增加了代码长度和阅读代码的难度。</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">二、用法:</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> BeanUtils</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">是这个包里比较常用的一个工具类,这里只介绍它的</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">copyProperties()</span><span style="letter-spacing:0.4pt;color:black;font-size:9pt;">方法。该方法定义如下:</span></p> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> public static void copyProperties(java.lang.Object dest,java.lang.Object orig)<br /> throws java.lang.IllegalAccessException,<br /> java.lang.reflect.InvocationTargetException</span> </div> </div> <div align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">如果你有两个具有很多相同属性的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">JavaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,一个很常见的情况就是</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">Struts</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">里的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">PO</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象(持久对象)和对应的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">ActionForm</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,例如</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> Teacher</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">和</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">TeacherForm</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">。我们一般会在</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">Action</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">里从</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">ActionForm</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">构造一个</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">PO</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象,传统的方式是使用类似下面的语句对属性逐个赋值:</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> //</span> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;">得到TeacherForm<br /> TeacherForm teacherForm=(TeacherForm)form;<br /> //构造Teacher对象<br /> Teacher teacher=new Teacher();<br /> //赋值<br /> teacher.setName(teacherForm.getName());<br /> teacher.setAge(teacherForm.getAge());<br /> teacher.setGender(teacherForm.getGender());<br /> teacher.setMajor(teacherForm.getMajor());<br /> teacher.setDepartment(teacherForm.getDepartment());<br /> <br /> //持久化Teacher对象到数据库<br /> HibernateDAO=;<br /> HibernateDAO.save(teacher);<br /> <br /> 而使用BeanUtils后,代码就大大改观了,如下所示:<br /> //得到TeacherForm<br /> TeacherForm teacherForm=(TeacherForm)form;<br /> //构造Teacher对象<br /> Teacher teacher=new Teacher();<br /> //赋值<br /> BeanUtils.copyProperties(teacher,teacherForm);<br /> //持久化Teacher对象到数据库<br /> HibernateDAO=;<br /> HibernateDAO.save(teacher);</span> </div> </div> <div align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">如果</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">Teacher</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">和</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">TeacherForm</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">间存在名称不相同的属性,则</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">不对这些属性进行处理,需要程序员手动处理。例如</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> Teacher</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">包含</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">modifyDate</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">(该属性记录最后修改日期,不需要用户在界面中输入)属性而</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">TeacherForm</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">无此属性,那么在上面代码的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> copyProperties()</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">后还要加上一句:</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> teacher.setModifyDate(new Date());</span> </div> </div> <div align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">怎么样,很方便吧!除</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">外还有一个名为</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">PropertyUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的工具类,它也提供</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">copyProperties()</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">方法,作用与</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发现两个</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">JavaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的同名属性为不同类型时,在支持的数据类型范围内进行转换,而前者不支持这个功能,但是速度会更快一些。</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">支持的转换类型如下:</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> * java.lang.BigDecimal<br /> * java.lang.BigInteger<br /> * boolean and java.lang.Boolean<br /> * byte and java.lang.Byte<br /> * char and java.lang.Character<br /> * java.lang.Class<br /> * double and java.lang.Double<br /> * float and java.lang.Float<br /> * int and java.lang.Integer<br /> * long and java.lang.Long<br /> * short and java.lang.Short<br /> * java.lang.String<br /> * java.sql.Date<br /> * java.sql.Time<br /> * java.sql.Timestamp </span> </div> </div> <div style="margin-bottom:12pt;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">这里要注意一点,</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">java.util.Date</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">是不被支持的,而它的子类</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">java.sql.Date</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">java.sql.Date</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">类型。否则在转换时会提示</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">argument mistype</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">异常。</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">三、优缺点:</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> Apache Jakarta Commons</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">项目非常有用。我曾在许多不同的项目上或直接或间接地使用各种流行的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">commons</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">组件。其中的一个强大的组件就是</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">。我将说明如何使用</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">将</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">local</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">实体</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">bean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">转换为对应的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">value </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象:</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> BeanUtils.copyProperties(aValue, aLocal)</span> </div> </div> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <p></p> <p></p> <p></p> <p></p> <div style="margin:3.75pt 0cm 2.25pt;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">有关动态</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">bean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的一些用法,涉及到的类如下:</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> <br /> 1</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">DynaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">是一个接口,它的实现类有</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BasicDynaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">LazyDynaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">ResultSetIterator</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">WrapDynaBean <br /> 2</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">DynaClass</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,也是接口,它的实现类有</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BasicDynaClass</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">WrapDynaClass</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">等</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> 3</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">DynaProperty</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,用来设置动态</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">bean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的属性</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> 4</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">PropertyUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,工具类,主要用来设置、取得属性值</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">等等,可以查阅</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">commons-beanutils.jar <br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">这里列举一个例子,讲述它的用法,代码如下:</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">①</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">动态设置了三个属性</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">id</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">name</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">、</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">email </span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> DynaProperty[] property = { <br /> new DynaProperty("id", Class.forName("java.lang.String")), <br /> new DynaProperty("name", Class.forName("java.lang.String")), <br /> new DynaProperty("email", Class.forName("java.lang.String")) <br /> }; </span> </div> </div> <div align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">②</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">创建一个</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">dynaClass</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,通过</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">dynaClass</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">来创建</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">DynaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的实例</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> DynaClass dynaClass = new BasicDynaClass("bean", null, property); <br /> DynaBean bean = dynaClass.newInstance(); </span> </div> </div> <div align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">③</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">通过</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">PropertyUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">来设置属性值</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> <a name="baidusnap0"></a><strong style="background-color:#ffff66;color:black;">PropertyUtils.setProperty</strong>(bean, "id", "001"); <br /> <strong style="background-color:#ffff66;color:black;">PropertyUtils.setProperty</strong>(bean, "name", "iplan"); <br /> <strong style="background-color:#ffff66;color:black;">PropertyUtils.setProperty</strong>(bean, "email", "l...@love.com"); </span> </div> </div> <div align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">④</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">通过以上的设置,这个</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">bean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">就可以像平时的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">javabean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">一样来使用了</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">当然,不是</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">xxx.getId()</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,而是</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">bean.get("id");</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的用法</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">说明:在</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">①</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">处,除了设置一般的类型外,也可以设置其它类型(如</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">java.util.List</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">等),还可以是一般的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">javabean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">。</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">在</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">②</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">处,在构造函数中的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">"bean"</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,是描述它的一个标记</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">在</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">③</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">处,可以不采用</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">PropertyUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">来设置属性值,而是直接通过如下方式</span> </div> <div style="border-bottom:#888888 1pt solid;border-left:#888888 1pt solid;padding-bottom:4pt;padding-left:4pt;padding-right:4pt;background:#d2e4fc;border-top:#888888 1pt solid;border-right:#888888 1pt solid;padding-top:4pt;"> <div style="border-bottom:medium none;text-align:left;border-left:medium none;padding-bottom:0cm;padding-left:0cm;padding-right:0cm;background:#d2e4fc;border-top:medium none;border-right:medium none;padding-top:0cm;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:12pt;"><br /> bean.set("id", "001"); <br /> bean.set("name", "iplan"); <br /> bean.set("email", "l...@love.com"); </span> </div> </div> <div style="margin:3.75pt 0cm 2.25pt;" align="left"> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">总结一下:要使用</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BasicDynaBean</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">,就首先要构造一个</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BasicDynaClass</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">来包含期望的数据结构。</span> </div> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">上面的代码从</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">aLocal</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象复制属性到</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">aValue</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象。它相当简单!它不管</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">local</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">(或对应的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">value</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">)对象有多少个属性,只管进行复制。我们假设</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">local</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象有</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">100</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">个属性。上面的代码使我们可以无需键入至少</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">100</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">行的冗长、容易出错和反复的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">get</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">和</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">set</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">方法调用。这太棒了!太强大了!太有用了!</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"><br /> <br /> </span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">现在,还有一个坏消息:使用</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">的成本惊人地昂贵!我做了一个简单的测试,</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">BeanUtils</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">所花费的时间要超过取数据、将其复制到对应的</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;"> value</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">对象(通过手动调用</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">get</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">和</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">set</span> <span style="letter-spacing:0.4pt;color:black;font-size:9pt;">方法),以及通过串行化将其返回到远程的客户机的时间总和。所以要小心使用这种威力!</span>
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!