BeanUtils tool class, beanutils tool class

Source: Internet
Author: User

BeanUtils tool class, beanutils tool class

1. BeanUtils Overview
BeanUtils is a set of tools provided by Apache to encapsulate some data into java objects;

Noun: javaBean: a java class in a specific format is called javaBean;

Requirements:
1: The serian class must implement the Serializable interface. (In actual development, it is usually omitted)
2: the javaBean must have the public permission to construct null parameters;
3: the javaBean must have the getXxx and setter methods corresponding to the property;

Ii. Use of BeanUtils
Beanutils has 2 dependent jar packages; commons-beanutils-1.8.3.jar and commons-logging-1.1.1.jar;
BeanUtils has two core classes: BeanUtils and ConvertUtils;
Procedure:
1. Download and decompress the package;
2: copy the core jar package to the project; (2)
3: add to local; (add to build path)
4: Use the core class;

Iii. Common BeanUtils Methods
Public static void setProperty (Object bean, String name, Object value)
Throws IllegalAccessException, InvocationTargetException {}: Save the value to the name attribute of the bean object;
Public static String getProperty (Object bean, String name)
Throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {}: get the name attribute value from the bean object;
Public static String [] getArrayProperty (Object bean, String name)
Throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {}: get the value of the name attribute array type from the bean object;
[Note: The getProperty method only recognizes the String type and String [] array type. It automatically converts other types to the two types. You must always think of the String type for use, package attributes with ""]
Public static void populate (Object bean, Map properties)
Throws IllegalAccessException, InvocationTargetException {}: match the data in the properties set according to the attribute name of the key and bean (actually matching the setXxx method). If the matching succeeds, the value is assigned. If the matching fails, no operation is performed;
Code demonstration 1: (the following code is fully implemented in Eclipse)

1 // create beanUtilsDemo01 package 2 package beanUtilsDemo01; 3 4 import java. util. arrays; 5 6 public class Person {7 // attribute 8 private String name; 9 private int age; 10 private String [] holobby; 11 12 // constructor 13 public Person () {14 super (); 15} 16 17 public Person (String name, int age, String [] holobby) {18 super (); 19 this. name = name; 20 this. age = age; 21 this. holobby = holobby; 22} 23 24 // getter/setter 25 p Ublic String getName () {26 return name; 27} 28 29 public void setName (String name) {30 this. name = name; 31} 32 33 public int getAge () {34 return age; 35} 36 37 public void setAge (int age) {38 this. age = age; 39} 40 41 public String [] getholobby () {42 return holobby; 43} 44 45 public void setholobby (String [] holobby) {46 this. holobby = holobby; 47} 48 49 // overwrite toString/equal/hashcode 50 @ Override 5 1 public String toString () {52 return "Person [name =" + name + ", age =" + age + ", holobby =" 53 + Arrays. toString (holobby) + "]"; 54} 55 56 @ Override 57 public int hashCode () {58 final int prime = 31; 59 int result = 1; 60 result = prime * result + age; 61 result = prime * result + Arrays. hashCode (holobby); 62 result = prime * result + (name = null )? 0: name. hashCode (); 63 return result; 64} 65 66 @ Override 67 public boolean equals (Object obj) {68 if (this = obj) {69 return true; 70} 71 if (obj = null) {72 return false; 73} 74 if (! (Obj instanceof Person) {75 return false; 76} 77 Person other = (Person) obj; 78 if (age! = Other. age) {79 return false; 80} 81 if (! Arrays. equals (holobby, other. holobby) {82 return false; 83} 84 if (name = null) {85 if (other. name! = Null) {86 return false; 87} 88} else if (! Name. equals (other. name) {89 return false; 90} 91 return true; 92} 93 94} 95 // create beanUtilsDemo01 package 96 package beanUtilsDemo01; 97 98 import java. util. arrays; 99 100 import org. apache. commons. beanutils. beanUtils; 101 102 // BeanUtils common methods exercise 103 104 public class Demo01BeanUtils {105 106 public static void main (String [] args) throws Exception {107 // instantiate object 108 Person p = new Person (); 109 // borrow the BeanUtils tool class to assign 110 BeanUtils to the Person object. setProperty (p, "name", "Rose"); 111 BeanUtils. setProperty (p, "age", 22); 112 BeanUtils. setProperty (p, "holobby", new String [] {"eating", "sleeping", 113 "kissing"}); 114 // print the object 115 System. out. println (p); 116 // obtain each attribute value 117 String [] holobby = BeanUtils. getArrayProperty (p, "holobby"); 118 System. out. println (Arrays. toString (holobby); 119 String name = BeanUtils. getProperty (p, "name"); 120 System. out. println (name); 121 String age = BeanUtils. get property (p, "age"); 122 System. out. println (age); 123} 124 125} 126


Code demonstration 2: encapsulate data in the map set

1 package beanUtilsDemo01; 2 3 import java. lang. reflect. invocationTargetException; 4 import java. util. hashMap; 5 import java. util. map; 6 7 import org. apache. commons. beanutils. beanUtils; 8 9 // use BeanUtils to encapsulate data in the Map into the javabean 10 11 public class Demo02BeanUtils {12 13 public static void main (String [] args) throws IllegalAccessException, 14 InvocationTargetException {15 // instantiate Object 16 Person p = new Person (); 17 // prepare MAP set 18 Map <String, Object> map = new HashMap <> (); 19 // Add data to map 20 map. put ("name", "jack"); 21 map. put ("age", 23); 22 map. put ("hobbyy", new String [] {"eating", "sleeping", "painting"}); 23 // encapsulate the data in the map set to 24 BeanUtils In the javabean. populate (p, map); 25 System. out. println (p); 26} 27} 28


Code demonstration 3: Use the same Person class ????????????????????????

1 package beanUtilsDemo01; 2 3 import java. util. hashMap; 4 import java. util. map; 5 6 import org. apache. commons. beanutils. beanUtils; 7 8 // use the BeanUtils tool class to customize the tool class: requires the input of any type of bytecode file and map set of attributes, returns the instantiated Object 9 class MyBeanUtils {10 public static <T> T popu (Class <T> c, Map map) throws Exception {// generic 11 Object obj = c. newInstance (); 12 BeanUtils. populate (obj, map); 13 return (T) obj; // downward transformation 14} 15} 16 17 public class MyTest {18 public static void main (String [] args) throws Exception {19 Map <String, Object> map = new HashMap <> (); 20 map. put ("name", "rose"); 21 map. put ("age", "18"); 22 Person p = MyBeanUtils. popu (Person. class, map); 23 System. out. println (p); 24} 25 26} 27


Code demonstration 4: Prepare a User class, the Person class above, and the data. xml file.

1 package beanutilcase; 2 3 import java. util. hashMap; 4 import java. util. list; 5 import java. util. map; 6 7 import org. apache. commons. beanutils. beanUtils; 8 import org. dom4j. document; 9 import org. dom4j. element; 10 import org. dom4j. io. SAXReader; 11 12 public class Demo {13 14 public static void main (String [] args) throws Exception {15 Person p = new Person (); 16 User u = new User (); 17 // create the parser object 18 SAXReader sax = new SAXReader (); 19 // read the document, and get the root node 20 Document doc = sax. read ("data. xml "); 21 Element root = doc. getRootElement (); 22 // obtain the level-1 sub-Element 23 List under the root node <Element> listFirst = root. elements (); 24 // iteration 25 for (Element e: listFirst) {26 // obtain the attribute value 27 String path = e for the first-level child Element. attributeValue ("className"); 28 // obtain the bytecode file 29 Class c = Class Based on the path (attribute. forName (path); 30 // obtain level-2 sub-Element 31 List <Element> listSecond = e. elements (); 32 // defines the value of 33 map <String, Object> Map = new HashMap <> () for the map set; 34 for (Element es: listSecond) {35 // obtain the two attribute values of the second-level child element 36 String name = es. attributeValue ("name"); 37 String value = es. attributeValue ("value"); 38 map. put (name, value); 39} 40 // use the beanutils tool class to encapsulate 41 // determine whether it is person 42 if (path. matches (". * Person $ ") {43 BeanUtils. populate (p, map); 44} else {45 BeanUtils. populate (u, map); 46} 47} 48 System. out. println (p); 49 System. out. println (u); 50} 51 52} 53

Related Article

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.