Usage of com.sun.org. Apache. commons. beanutils. beanutils

Source: Internet
Author: User
I. Introduction:

Beanutils provides packaging for Java reflection and introspection APIs. The main purpose is to use the reflection mechanism to process attributes of JavaBean. We know that a JavaBean usually contains a large number of attributes. In many cases, processing of JavaBean leads to a large number of get/set code accumulation, increasing the length of the Code and the difficulty of reading the code.

Ii. Usage: beanutils is a commonly used tool class in this package. Here we only introduce its copyproperties () method. This method is defined as follows: public static void copyproperties (Java. lang. object DEST, Java. lang. object orig) throws Java. lang. illegalaccessexception, Java. lang. reflect. invocationtargetexception if you have two Java Beans with many identical attributes, a common situation is the Po object (Persistent Object) in struts and the corresponding actionform, such as teacher and teacherform. Generally, we construct a po object from actionform in action. The traditional method is to assign values to attributes one by one using statements similar to the following: // get teacherformteacherform teacherform = (teacherform) form; // construct the teacher object Teacher = new teacher (); // assign a value to teacher. setname (teacherform. getname (); teacher. setage (teacherform. getage (); teacher. setgender (teacherform. getgender (); teacher. setmajor (teacherform. getmajor (); teacher. setdepartment (teacherform. getdepartment (); // persists the teacher object to the database hibernatedao =; Hibernatedao. save (teacher); after using beanutils, the code is greatly improved, as shown below: // get teacherformteacherform teacherform = (teacherform) form; // construct the teacher object Teacher = new teacher (); // assign the value to beanutils. copyproperties (teacher, teacherform); // persists the teacher object to the database hibernatedao =; hibernatedao. save (teacher); if teacher and teacherform have attributes with different names, beanutils will not process these attributes, which must be manually processed by the programmer. For example, if teacher contains the modifydate attribute (the last modification date of this attribute record, which does not need to be input on the Interface) and teacherform does not have this attribute, then the copyproperties () in the above Code () add the following sentence: Teacher. how about setmodifydate (new date (); very convenient! In addition to beanutils, there is also a tool class named propertyutils. It also provides the copyproperties () method, which is similar to the beanutils method with the same name. The main difference is that the latter provides the type conversion function, that is to say, if the attributes with the same name of two JavaBean objects are of different types, they are converted within the supported data type range. The former does not support this function, but the speed is faster. Beanutils supports the following conversion types: * Java. lang. bigdecimal * Java. lang. biginteger * Boolean and Java. lang. boolean * byte and Java. lang. byte * char and Java. lang. character * Java. lang. class * double and Java. lang. double * float and Java. lang. float * int and Java. lang. integer * long and Java. lang. long * short and Java. lang. short * Java. lang. string * Java. SQL. date * Java. SQL. time * Java. SQL. timestamp

Note that Java. util. date is not supported, and its subclass java. SQL. date is supported. Therefore, if the object contains time-type attributes and you want to convert them, you must use the java. SQL. date type. Otherwise, an argument mistype exception occurs during conversion.

Iii. Advantages and Disadvantages:

The Apache Jakarta Commons project is very useful. I have used various popular commons components directly or indirectly on many different projects. One of the powerful components is beanutils. I will explain how to use beanutils to convert the local entity bean to the corresponding value object:

BeanUtils.copyProperties(aValue, aLocal)

The above code copies attributes from the alocal object to the avalue object. It's quite simple! No matter how many attributes the local (or corresponding value) object has, it just copies the object. Assume that the local object has 100 attributes. The code above allows us to avoid having to enter at least 100 lines of lengthy, error-prone, and repeated get and set method calls. This is amazing! It's so powerful! So useful!

There is another bad news: the cost of using beanutils is surprisingly expensive! I did a simple test. beanutils took more time to retrieve data and copy it to the corresponding value object (by manually calling the get and set methods ), and the total time it takes to return to the remote client through serialization. So be careful with this power!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.