In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the usage and rewriting of Beanutils.copyProperties () to improve efficiency". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of Beanutils.copyProperties () and rewriting to improve efficiency".
Catalogue
Beanutils.copyProperties () usage and rewriting to improve efficiency
I. brief introduction
II. Usage
3. Rewrite
Principle
Note when using BeanUtils.copyProperties
Sample demonstration
Start with the example
Finally, it is emphasized
Beanutils.copyProperties () usage and rewriting to improve efficiency
In particular, this paper introduces the BeanUtils.copyProperties (import org.springframework.beans.BeanUtils B) method in Spring (import org.springframework.beans.BeanUtils). Is to assign the value in A to B. The BeanUtils.copyProperties method in apache (org.apache.commons.beanutils.BeanUtils) is to assign the value in B to A.
I. brief introduction
BeanUtils provides wrappers for Java reflection and introspection API. Its main purpose is to use the reflection mechanism to deal with the attributes of JavaBean. We know that a JavaBean usually contains a large number of attributes, and in many cases, the processing of JavaBean leads to a large accumulation of get/set code, increasing the length of the code and the difficulty of reading the code.
II. Usage
If you have two JavaBean with many of the same properties, a common situation is the PO object (persistent object) in Struts and the corresponding ActionForm. For example, a user registration page has a User entity class and a UserActionForm. We usually construct a PO object from ActionForm in Action. The traditional way is to use statements like the following to assign values to attributes one by one:
/ / get ActionForm form data UserActionForm uForm = (UserActionForm) form; / / construct a User object User user = new User (); / / assign user.setUsername (uForm.getUsername) one by one; user.setPassword (uForm.getPassword); user.setAge (uForm.getAge);. . / / then call JDBC, or operate Hibernate to persist the object User to the database HibernateDAO.save (user)
In this way, if the form data N, 100, 1000 (exaggeration point. (), so we are not going to write 100, 1000 lines of set get. Everyone
I don't want to do this.
After we use the BeanUtils.copyProperties () method, the amount of code is greatly reduced, and the whole program looks concise and clear, the code is as follows:
/ / get ActionForm form data UserActionForm uForm = (UserActionForm) form; / / construct a User object User user = new User (); BeanUtils.copyProperties (uForm,user); / / then call JDBC, or manipulate Hibernate to persist the object User to the database HibernateDAO.save (user)
Note: if there are attributes with different names between User and UserActionForm, BeanUtils does not process these attributes and requires manual processing. For example:
There is a createDate creation time field in the User class, but not in UserActionForm. BeanUtils.copyProperties () does nothing with this field. You have to handle it manually.
User.setModifyDate (new Date ()); III. Rewrite
ReflectASM, high performance reflection:
What is ReflectASM ReflectASM is a very small java class library, mainly through the asm production class to achieve java reflection, the execution speed is very fast, read a lot of online and reflection comparison, think that ReflectASM is more magical, would like to know its principle, the following describes how to use and principle
Public static void main (String [] args) {User user = new User (); / / use reflectasm to produce User access class MethodAccess access = MethodAccess.get (User.class); / / invoke setName method name value access.invoke (user, "setName", "Zhang San") / / invoke getName method to get the value String name = (String) access.invoke (user, "getName", null); System.out.println (name);} principle
The above code does realize the function of reflection, and the main core of the code is MethodAccess.get (User.class).
Look at the source code, this code is mainly through asm to produce a User processing class UserMethodAccess (this class mainly implements the invoke method) ByteCode, and then get the object, through the above invoke operation user class.
Private static Map methodMap = new HashMap (); private static Map methodIndexMap = new HashMap (); private static Map fieldMap = new HashMap (); public static void copyProperties (Object desc, Object orgi) {MethodAccess descMethodAccess = methodMap.get (desc.getClass ()); if (descMethodAccess = = null) {descMethodAccess = cache (desc);} MethodAccess orgiMethodAccess = methodMap.get (orgi.getClass ()) If (orgiMethodAccess = = null) {orgiMethodAccess = cache (orgi);} List fieldList = fieldMap.get (orgi.getClass ()); for (String field: fieldList) {String getKey = orgi.getClass (). GetName () + "." + "get" + field; String setkey = desc.getClass (). GetName () + "." + "set" + field Integer setIndex = methodIndexMap.get (setkey); if (setIndex! = null) {int getIndex = methodIndexMap.get (getKey) / / Parameter 1 object to be reflected / / Parameter 2 class.getDeclaredMethods corresponding method index / / parameter pair three image sets descMethodAccess.invoke (desc, setIndex.intValue (), orgiMethodAccess.invoke (orgi, getIndex)) } / / Singleton pattern private static MethodAccess cache (Object orgi) {synchronized (orgi.getClass ()) {MethodAccess methodAccess = MethodAccess.get (orgi.getClass ()); Field [] fields = orgi.getClass () .getDeclaredFields (); List fieldList = new ArrayList (fields.length) For (Field field: fields) {if (Modifier.isPrivate (field.getModifiers ()) & &! Modifier.isStatic (field.getModifiers () {/ / whether it is private and static / / non-public private variable String fieldName = StringUtils.capitalize (field.getName ()) / / get the attribute name int getIndex = methodAccess.getIndex ("get" + fieldName); / / get the subscript int setIndex = methodAccess.getIndex ("set" + fieldName) of the get method / / get the subscript methodIndexMap.put of the set method (orgi.getClass (). GetName () + "." + "get" + fieldName, getIndex) / / register the class name get method name, method subscript to map methodIndexMap.put (orgi.getClass (). GetName () + "." + "set" + fieldName, setIndex); / / register the class name set method name, method subscript to map fieldList.add (fieldName) / / put the attribute name in the collection}} fieldMap.put (orgi.getClass (), fieldList); / / register the class name, attribute name with the map methodMap.put (orgi.getClass (), methodAccess); return methodAccess;}}
The execution of 1000000 items of efficiency is more than 80 milliseconds, and the efficiency is no problem.
Note when using BeanUtils.copyProperties
First of all, it is concluded that BeanUtils.copyProperties is a shallow copy.
Why do I still want to use this BeanUtils.copyProperties for military training today?
Because I realized that everyone (in part) is still not clear about deep handcuffs and shallow handcuffs, and I don't know the specific impact.
Sample demonstration
The first class:
The second class:
Attention! The second class uses the first class.
Start with the example public static void main (String [] args) {/ * Analog data A complexObject * / ComplexObject complexObjectA=new ComplexObject (); complexObjectA.setNickName ("Zhang Yi"); SimpleObject simpleObject=new SimpleObject (); simpleObject.setName ("Li Si"); simpleObject.setAge (12); complexObjectA.setSimpleObject (simpleObject) / * * use BeanUtils.copyProperties to copy simulation data A to generate simulation data B * / ComplexObject complexObjectB=new ComplexObject (); BeanUtils.copyProperties (complexObjectA,complexObjectB); System.out.println ("after copying, view simulation data An and simulation data B:"); System.out.println (complexObjectA.getSimpleObject (). ToString ()) System.out.println (complexObjectB.getSimpleObject (). ToString ()); System.out.println ("compare whether the reference object simple in simulation data An and simulation data B has the same reference address:"); System.out.println (complexObjectA.getSimpleObject () = = complexObjectB.getSimpleObject ()); System.out.println ("modify the attribute age of reference object simple in copied simulation data B is 888888") ComplexObjectB.getSimpleObject (). SetAge (888888); System.out.println ("after modification, observe the attribute age of the object simple referenced in the original data An and the copied data B:"); System.out.println (complexObjectA.getSimpleObject (). ToString ()); System.out.println (complexObjectB.getSimpleObject (). ToString ());}
Finally, it is emphasized
If you are using BeanUtils.copyProperties to copy and copy objects, be careful!
First, the package you copied does not contain references to other objects.
Second, if it is included, then whether the operation of the original object or the copied object involves the modification of the value of that other object in the next method.
Third, if it is involved, whether it will affect the use of modified values by other methods.
As an example, if a complex object data A refers to a user object whose age age is 10; after copying a copy of data B, the method of manipulating data B changes the age age to 88888
Then other subsequent methods use data An and want to use the original age of 10, so it is not necessary. Because the shallow copy is affected, the age has become 88888.
Thank you for your reading, the above is the content of "Beanutils.copyProperties () usage and rewriting to improve efficiency". After the study of this article, I believe you have a deeper understanding of the use of Beanutils.copyProperties () and rewriting to improve efficiency, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un