First, Introduction:
Beanutils provides packaging for the Java reflection and introspection API. Its main purpose is to deal with the properties of JavaBean by reflection mechanism. We know that a javabean usually contains a lot of attributes, and in many cases, the processing of JavaBean leads to a lot of get/set code buildup, increasing the code length and the difficulty of reading the code.
Second, usage:
If you have two JavaBean with many of the same attributes, a common case is the Po object (persistent object) and the corresponding actionform in struts. For example: A user registration page, a users entity class and a useractionform, we typically construct a Po object from Actionform in action, the traditional way is to assign a value to a property individually using a statement like this:
[HTML]View PlainCopy
- Get actionform form data
- Useractionform uform = (useractionform) Form;
- Constructs a User object
- User user = new user ();
- Assign values individually
- User.setusername (Uform.getusername);
- User.setpassword (Uform.getpassword);
- User.setage (Uform.getage);
- Then call JDBC, or manipulate Hibernate persisted object user to database
In this way, if the form data is more than N, 100, 1000 (exaggeration point. haha) 、、、、 then we're not going to write 、、、 1000 rows set, get. No one is willing to do so.
And we use the Beanutils.copyproperties () method, the amount of code is greatly reduced, and the overall program looks concise and clear, the code is as follows:
[HTML]View PlainCopy
- Get actionform form data
- Useractionform uform = (useractionform) Form;
- Constructs a User object
- User user = new user ();
- Assign value
- Beanutils.copyproperties (user, uform);
- Then call JDBC, or manipulate Hibernate persisted object user to database
- .......
Note: If there are properties with different names between user and Useractionform, Beanutils does not handle these properties and needs to be handled manually. For example:
The user class has a CreateDate creation time field, and Useractionform does not have this field. Beanutils.copyproperties () does not do any processing on this field. Must be handled manually by yourself.
Copy will error when date is null
Commons-beanutils, however, imposes many tests, including the conversion of types, and even examines the accessibility of the class to which the object belongs.
In addition, the commons-beanutils in the java.util.Date is not supported. In addition to supporting basic types and arrays of basic types, java.sql.Date, Java.sql.Time, Java.sql.TimeStamp, Java.io.File, Javaio are also supported. URLs for these classes of objects are not supported by the rest. But you can customize the converter of your class. Then sign in.
Feel Commons-beanutils package of this Beanutils class Copyproperties method, too complex, too many constraints, and inconvenience, although the scalability is good, but the ease of use is not high.
Summarize:
With regard to bean replication, if there are fewer attributes, it is recommended to write a method directly to complete get/set. If there are many attributes, you can use reflection to implement a tool class that satisfies your needs, or using the spring Beanutils class, it is not recommended to use the Beanutils class in the Commons-beanutils package, just looked at, This class also has problems with object replication for internal static classes, which are too complex to be tested and often have some weird problems. After all, our bean copy is generally a simple attribute copy.
Moreover, because these beanutils classes are implemented by reflection mechanism, it also affects the efficiency of the program. Therefore, careful use of beanutils.copyproperties!!!
In addition to Beanutils, there is a tool class called Propertyutils, which also provides the Copyproperties () method, which is very similar to the Beanutils method of the same name, the main difference being that the latter provides the type conversion function, That is, when the same name attribute of two JavaBean is found to be of different types, it is converted within the range of supported data types, while the former does not support this function, but it is faster
Beanutils.copyproperties () Usage of Java object copy