Java-usage and difference of BeanUtils.copyProperties and PropertyUtils.copyProperties

1. Introduction

BeanUtils provides packaging for Java reflection and introspection API. Its main purpose is to use the reflection mechanism to process the properties of JavaBean. We know that a JavaBean usually contains a large number of attributes. In many cases, the processing of JavaBean causes a large number of get / set codes to accumulate, which increases the length of the code and the difficulty of reading the code. It needs the support of Collections package and logging package.

 

Second, usage

BeanUtils is a more commonly used tool class in this package, only its copyProperties () method is introduced here. The method is defined as follows (source is assigned to target)

public static void copyProperties(Object source, Object target)
 throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException

If you have two JavaBeans with many 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. The traditional way is to assign attributes one by one using statements similar to the following:

//得到TeacherForm
TeacherForm teacherForm=(TeacherForm)form;

//构造Teacher对象
Teacher teacher=new Teacher();

//赋值
teacher.setName(teacherForm.getName());
teacher.setAge(teacherForm.getAge());
teacher.setGender(teacherForm.getGender());
teacher.setMajor(teacherForm.getMajor());
teacher.setDepartment(teacherForm.getDepartment());

//持久化Teacher对象到数据库
HibernateDAO.save(teacher);

After using BeanUtils, the code has changed a lot, as shown below:

//得到TeacherForm
TeacherForm teacherForm=(TeacherForm)form;

//构造Teacher对象
Teacher teacher=new Teacher();

//赋值
BeanUtils.copyProperties(teacher,teacherForm);

//持久化Teacher对象到数据库
HibernateDAO.save(teacher);

If there are attributes with different names between Teacher and TeacherForm, BeanUtils does not process these attributes, and the programmer needs to deal with them manually. For example, the Teacher contains the modifyDate (this property records the last modification date, and does not require the user to enter it in the interface) property and the TeacherForm does not have this property, then add a sentence after copyProperties () in the above code:

teacher.setModifyDate(new Date());

How is it convenient! In addition to BeanUtils, there is a tool class called PropertyUtils, which also provides copyProperties () method, which is very similar to the method of the same name in BeanUtils. The main difference is that the latter provides type conversion functions, that is, the two JavaBeans have the same name property For different types, conversion is performed within the range of supported data types, and the former does not support this function, but the speed will be faster. The conversion types supported by BeanUtils are 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 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 

One thing to note here is that java.util.Date is not supported, and its subclass java.sql.Date is supported. Therefore, if the object contains a time type attribute and you want to be converted, you must use the java.sql.Date type. Otherwise, it will prompt an argument mistype exception during conversion.

 

Three, 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 local entity beans into corresponding value objects:

BeanUtils.copyProperties(aValue, aLocal)

The above code copies properties from aLocal object to aValue object. It's pretty simple! It doesn't matter how many attributes the local (or corresponding value) object has, just copy it. We assume that the local object has 100 attributes. The above code allows us to avoid lengthy, error-prone and repeated get and set method calls without typing at least 100 lines. This is awesome! Too strong! It's so useful!

Now, there is the bad news: the cost of using BeanUtils is surprisingly expensive! I did a simple test. BeanUtils took more time than fetching data, copying it to the corresponding value object (by manually calling get and set methods), and returning it to the remote client through serialization. The sum of time. So be careful when using this power!

Published 952 original articles · praised 1820 · 890,000 views

Guess you like

Origin blog.csdn.net/Dream_Weave/article/details/105374400