The usage and efficiency of beanutils.copyproperties () method

Source: Internet
Author: User

1.BeanUtils provides packaging for the Java reflection and introspection APIs. Its main purpose is to use the reflection mechanism to deal with the properties of JavaBean. The Po object and the Vo object that we often use in the project have the same properties in many cases, and the Beanutils.copyproperties () method can reduce the programmer's Get/set method again, beanutils.copyproperties


() method also has its advantages and disadvantages, below we through the program to verify.




2. First we create two classes: Studen.java and Studentvo.java two Student.java package my.test;


public class Student {private Integer ID;


Private String sname;


Private String spwd;




Private String saddress;


Public Integer GetId () {return id;


The public void SetId (Integer id) {this.id = ID;


Public String Getsname () {return sname;


} public void Setsname (String sname) {this.sname = sname;


Public String getspwd () {return spwd;


} public void Setspwd (String spwd) {this.spwd = spwd;


Public String getsaddress () {return saddress;


} public void Setsaddress (String saddress) {this.saddress = saddress;




}} Studentvo.java package my.test;


public class Studentvo {private Integer ID;


Private String sname;


Private String spwd;




Private String saddress; Public IntegerGetId () {return id;


The public void SetId (Integer id) {this.id = ID;


Public String Getsname () {return sname;


} public void Setsname (String sname) {this.sname = sname;


Public String getspwd () {return spwd;


} public void Setspwd (String spwd) {this.spwd = spwd;


Public String getsaddress () {return saddress;


} public void Setsaddress (String saddress) {this.saddress = saddress;


}








}




You can see that these two classes have the same properties


Test.java


Package my.test;




Import java.lang.reflect.InvocationTargetException;




Import Org.apache.commons.beanutils.BeanUtils;


public class Test {public static void main (string[] args) {Student stu = new Student ();


Stu.setid (2);


Stu.setsname ("Zhangsan");


Stu.setsaddress ("Shanghai");


Stu.setspwd ("QWEASD");




Studentvo Stuvo = new Studentvo ();


Long start = System.currenttimemillis ();


Stuvo.setid (Stu.getid ());


Stuvo.setsname (Stu.getsname ());


Stuvo.setspwd (Stu.getspwd ());


Stuvo.setsaddress (Stu.getsaddress ());


Long end = System.currenttimemillis ();


Long time2 = End-start;


SYSTEM.OUT.PRINTLN ("The result of using the Get/set method:");


System.out.println (time2 + "MS");


System.out.println (Stuvo.getid ());


System.out.println (Stuvo.getsname ());


System.out.println (Stuvo.getspwd ());




System.out.println (Stuvo.getsaddress ());


System.out.println ("----------------------");


Long time1 = usecoryproperties (Stuvo, Stu);


SYSTEM.OUT.PRINTLN ("Use the result of the Beanutils.copyproperties () method:"); System.Out.println (time1 + "MS");


System.out.println (Stuvo.getid ());


System.out.println (Stuvo.getsname ());


System.out.println (Stuvo.getspwd ());


System.out.println (Stuvo.getsaddress ());


System.out.println (Stuvo.getdate ());


private static long Usecoryproperties (object A, object B) {Long start = System.currenttimemillis ();


try {beanutils.copyproperties (A, b);


catch (Illegalaccessexception e) {//TODO auto-generated catch block E.printstacktrace ();


catch (InvocationTargetException e) {//TODO auto-generated catch block E.printstacktrace ();


Long end = System.currenttimemillis ();


return end-start;


}


}






Output results:




Results of using the Get/set method:


0ms


2


zhangsan


qweasd


Shanghai


----------------------


Results of using the Beanutils.copyproperties () method:


93ms


2


zhangsan


QWEASD










We can see from the result that the Beanutils.copyproperties () method can be used to copy the properties of the student class, and also to obtain the values of the set.


But we see the low efficiency, the use of the Get/set method basically does not consume time, but the use of the Beanutils.copyproperties () method is time-consuming to achieve


99MS, this only sets a few attributes, can be learned that when the number of attributes more and more time will be more and more consumption, but it saves manual operation.




Limitations of the 3.beanutils.copyproperties () method


Processing is done only when the properties of the two classes are the same, not for the different properties, not for the Java.util.Date class, but for the java.sql.Date, and for the Integer,boolean,long type.


The default value is NULL, but the default value is 0 after the method is used, so be careful. Use is also relatively low efficiency, so be cautious to use. When two classes are together this method does not, as follows




  Class studentvo{...  


  }  


  Class student{...  


  }  


   


  public class Test {public  


      static void Main (string[] args) {  


          ...  


      }  


  }  




4. Jar Package Required


Commons-beanutils-*.jar


Commons-logging-*.jar


























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.