Use propertyutils and methodutils

Source: Internet
Author: User
ArticleDirectory
    •  
Java Code
  1. PackageRong. propertyutils;
  2. ImportJava. util. Map;
  3. ImportOrg. Apache. commons. beanutils. methodutils;
  4. ImportOrg. Apache. commons. beanutils. propertyutils;
  5. Public ClassTestpropertyutils {
  6. Public Static VoidMain (string [] ARGs)ThrowsException {
  7. Entity entity =NewEntity ();
  8. // Obtain the value of the specified property through the getproperty method of propertyutils
  9. Integer id = (integer) propertyutils. getproperty (entity,"ID");
  10. String name = (string) propertyutils. getproperty (entity,"Name");
  11. System. Out. println ("Id ="+ ID +"Name ="+ Name );
  12. // Call the setproperty method of propertyutils to set the specified attribute of Entity
  13. Propertyutils. setproperty (entity,"Name","Mxengfan Ying");
  14. System. Out. println ("Name ="+ Entity. getname ());
  15. // Encapsulate all attributes and attribute values of entity into map using the Describe Method of propertyutils
  16. Map map = propertyutils. Describe (entity );
  17. System. Out. println ("Id ="+ Map. Get ("ID") +"Name ="+ Map. Get ("Name"));
  18. // Use the invokemethod method of methodutils to execute the method in the specified entity (without parameters)
  19. System. Out. println (methodutils. invokemethod (entity,"Haha",Null));
  20. // Use the invokemethod method of methodutils to execute the method in the specified entity (1 parameter)
  21. Methodutils. invokemethod (entity,"Sayhelle","Mxengfan Ying");
  22. // Use the invokemethod method of methodutils to execute the method in the specified entity (multiple parameters)
  23. Object [] Params =NewObject [] {NewINTEGER (10),NewINTEGER (12)};
  24. String MSG = (string) methodutils. invokemethod (entity,"Countages", Params );
  25. System. Out. println (MSG );
  26. }
  27. }
Package Rong. propertyutils; import Java. util. map; import Org. apache. commons. beanutils. methodutils; import Org. apache. commons. beanutils. propertyutils; public class testpropertyutils {public static void main (string [] ARGs) throws exception {entity = new entity (); // use the getproperty method of propertyutils to obtain the value of the specified property integer id = (integer) propertyutils. getproperty (entity, "ID"); string name = (string) propertyutils. getproperty (entity, "name"); system. out. println ("ID =" + ID + "name =" + name); // call the setproperty method of propertyutils to set the specified property propertyutils of the entity. setproperty (entity, "name", "Dream Fan Ying"); system. out. println ("name =" + entity. getname (); // encapsulate all attributes and attribute values of the entity into map = propertyutils through the Describe Method of propertyutils. describe (entity); system. out. println ("ID =" + map. get ("ID") + "name =" + map. get ("name"); // use the invokemethod method of methodutils to execute the method in the specified entity (if no parameter exists) system. out. println (methodutils. invokemethod (entity, "Haha", null); // use the invokemethod method of methodutils to execute methodutils in the specified entity (1 parameter. invokemethod (entity, "sayhelle", "mxengfan Ying"); // use the invokemethod method of methodutils to execute the method in the specified entity (multi-parameter condition) object [] Params = new object [] {New INTEGER (10), new INTEGER (12)}; string MSG = (string) methodutils. invokemethod (entity, "countages", Params); system. out. println (MSG );}}

id = 5 name = rongxinhua
name = xinmengfan Ying
id = 5 name = xinmengfan Ying
HA, HA
xinmengfan Ying say, hello!
my age is 22

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.