Org.apache.commons.beanutils.BeanUtils Introduction

xiaoxiao2021-04-08  305

Beanutils simple reading

I. Introduction:

Beanutils provides packaging for Java reflection and self-provincial API. Its main purpose is to use the reflective mechanism to handle the properties of JavaBeans. We know that a JavaBean typically contains a lot of properties. In many cases, the processing of JavaBean results in a large number of GET / SET code accumulation, increasing the length of code length and reading code.

Second, usage:

BeanUtils is a tool class that is more common in this package. Here is only 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.InvocationTARGETEXCEPTIONTARGETEXCEPTION

If you have two JavaBeans with a lot of the same properties, a very common situation is the PO object (persistent object) in Struts and the corresponding ActionForm, such as Teacher and Teacherform. We generally construct a PO object from ActionForm in Action, and traditional ways are assigned one by one similar to the following statement:

// get TeacherFormTeacherForm teacherForm = (TeacherForm) form; // objects configured Teacher Teacher teacher = new Teacher (); // Assignment teacher.setName (teacherForm.getName ()); teacher.setAge (teacherForm.getAge ()); teacher .setGender (teacherForm.getGender ()); teacher.setMajor (teacherForm.getMajor ()); teacher.setDepartment (teacherForm.getDepartment ()); // Teacher persistent objects to the database HibernateDAO =; HibernateDAO.save (teacher); After using beanutils, the code is greatly changed, as shown below: // Get TeacherFormTeacherform Teacherform = (Teacherform) Formher; // Constructs Teacher Object Teacher Teacher = New Teacher (); // Assignment Bean Autils.copyProperties (Teacher, Teacherform) ; // Hold the TEACHER object to the database hibernatedao =; hibernatedao.save (teacher);

If Teacher and TeacherForm have a name different from the same name, beanUtils does not process these properties, requiring programmers to manually process. For example, Teacher contains modifydate (this property record last modified date, no user input) attribute in the interface) attribute, then addproperties (), then addProperties after the above code:

Teacher.SetModifyDate (New Date ());

How is it, it is very convenient! In addition to BeanUtils, there is a tool class named PropertyUtils. It also provides a copyproperties () method, and the role is very similar to BeanUtils's same name method, the main difference is that the latter provides type conversion function, that is, found two JavaBean's same name properties Different types of conversions in the supported data types, while the former does not support this feature, but the speed will be faster. The conversion type supported by BeanUtils is as follows: * 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 ANDA.lang.integer * long and java.lang.long * short and java.lang.short * java.lang.string * java.sql. Date * java.sql.time * java.sql.timestamp

It is important to note that java.util.date is not supported, and its subclavab java.sql.date is supported. So if the object contains the attribute of the time type, and when it is desired to be converted, you must use the java.sql.date type. Otherwise, the Argument MISTYPE is prompted at the time of conversion.

Third, advantages and disadvantages:

The Apache Jakarta Commons project is very useful. I have used various popular Commons components on many different items or directly or indirectly. One of the powerful components is BeanUtils. I will explain how to convert the Local entity bean to the corresponding Value object using beanUtils:

Beanutils.copyProperties (Avalue, Alocal)

The above code is replicated from the Alocal object to the Avalue object. It is quite simple! It doesn't matter how many attributes have LOCAL (or corresponding value) objects, only copies. We assume that the local object has 100 properties. The above code allows us to do not need to type at least 100 rows of lengthy, easy error and repeated GET and SET method calls. This is great! Too strong! Too useful!

Now, there is a bad news: the cost of using beanutils is amazing! I have made a simple test, and the time spent by beanutils exceeds the data, copy it to the corresponding value object (by manually calling the GET and SET methods), and returning it to the remote client by serialization Time sum. So be careful to use this power!

转载请注明原文地址:https://www.9cbs.com/read-132674.html

New Post(0)