BeanUtils 工具類,beanutils工具類
一.BeanUtils 概述
BeanUtils 是阿帕奇提供的一套專門用於將一些資料封裝到java對象中的工具類;
名詞:javaBean:特定格式的java類稱為javaBean;
要求:
1:javaBean這個類要實現Serializable介面;(在實際開發中,通常省略了)
2:javaBean必須有public許可權的空參數的構造方法;
3:javaBean必須有屬性對應的getXxx與setter方法;
二.BeanUtils 的使用
Beanutils 有2個依賴jar包;commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar;
BeanUtils 有2個核心類:BeanUtils,ConvertUtils 類;
使用步驟:
1:下載解壓;
2:複製核心jar包到工程中;(有2個)
3:添加到本地;(add to build path)
4:使用核心類;
三.BeanUtils 常用方法
public static void setProperty(Object bean,String name,Object value)
throws IllegalAccessException,InvocationTargetException{}:向bean對象的name屬性中儲存value值;
public static String getProperty(Object bean,String name)
throws IllegalAccessException,InvocationTargetException,NoSuchMethodException{}:從bean對象中擷取name屬性的值;
public static String[] getArrayProperty(Object bean,String name)
throws IllegalAccessException,InvocationTargetException,NoSuchMethodException{}:從bean對象中擷取name屬性的數群組類型的值;
[注:getProperty方法就只認String類型和String[]數群組類型,其它類型它會自動幫你轉成這兩個類型,使用時需時刻想到String類型,用""包裹屬性]
public static void populate(Object bean,Map properties)
throws IllegalAccessException,InvocationTargetException{}:將properties集合中的資料,根據key與bean的屬性名稱(實際上是匹配setXxx方法) 匹配,匹配成功,則賦值,匹配失敗不操作;
代碼示範1:(以下代碼全在Eclipse中實現)
1 //建立beanUtilsDemo01包 2 package beanUtilsDemo01; 3 4 import java.util.Arrays; 5 6 public class Person { 7 // 屬性 8 private String name; 9 private int age; 10 private String[] hobby; 11 12 // 構造方法 13 public Person() { 14 super(); 15 } 16 17 public Person(String name, int age, String[] hobby) { 18 super(); 19 this.name = name; 20 this.age = age; 21 this.hobby = hobby; 22 } 23 24 // getter/setter 25 public 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[] getHobby() { 42 return hobby; 43 } 44 45 public void setHobby(String[] hobby) { 46 this.hobby = hobby; 47 } 48 49 // 覆寫toString/equal/hashcode 50 @Override 51 public String toString() { 52 return "Person [name=" + name + ", age=" + age + ", hobby=" 53 + Arrays.toString(hobby) + "]"; 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(hobby); 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(hobby, other.hobby)) { 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 //建立beanUtilsDemo01包 96 package beanUtilsDemo01; 97 98 import java.util.Arrays; 99 100 import org.apache.commons.beanutils.BeanUtils;101 102 //BeanUtils常用方法練習103 104 public class Demo01BeanUtils {105 106 public static void main(String[] args) throws Exception {107 // 執行個體化對象108 Person p = new Person();109 // 借用BeanUtils工具類向Person對象賦值110 BeanUtils.setProperty(p, "name", "Rose");111 BeanUtils.setProperty(p, "age", 22);112 BeanUtils.setProperty(p, "hobby", new String[] { "eating", "sleeping",113 "kissing" });114 // 列印對象115 System.out.println(p);116 // 擷取各屬性值117 String[] hobby = BeanUtils.getArrayProperty(p, "hobby");118 System.out.println(Arrays.toString(hobby));119 String name = BeanUtils.getProperty(p, "name");120 System.out.println(name);121 String age = BeanUtils.getProperty(p, "age");122 System.out.println(age);123 }124 125 }126
代碼示範2:封裝map集合中的資料
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 //借用BeanUtils將Map中的資料封裝到javabean中 10 11 public class Demo02BeanUtils { 12 13 public static void main(String[] args) throws IllegalAccessException, 14 InvocationTargetException { 15 // 執行個體化對象 16 Person p = new Person(); 17 // 準備MAP集合 18 Map<String, Object> map = new HashMap<>(); 19 // 向map中添加資料 20 map.put("name", "jack"); 21 map.put("age", 23); 22 map.put("hobbyy", new String[] { "eating", "sleeping", "painting" }); 23 // 將map集合中的資料封裝到javabean中 24 BeanUtils.populate(p, map); 25 System.out.println(p); 26 } 27 } 28
代碼示範3:與以上利用同一個Person類????????????????????????
1 package beanUtilsDemo01; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.apache.commons.beanutils.BeanUtils; 7 8 //利用BeanUtils工具類自訂工具類:要求傳入任一類型的位元組碼檔案 和 屬性的map集合,返回執行個體化對象 9 class MyBeanUtils { 10 public static <T> T popu(Class<T> c, Map map) throws Exception { //泛型 11 Object obj = c.newInstance(); 12 BeanUtils.populate(obj, map); 13 return (T) obj; //向下轉型 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
代碼示範4:需準備一個User類,和以上的Person類,及data.xml檔案
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 // 建立解析器對象 18 SAXReader sax = new SAXReader(); 19 // 讀取文檔,並擷取根節點 20 Document doc = sax.read("data.xml"); 21 Element root = doc.getRootElement(); 22 // 擷取根節點下的一級子項目 23 List<Element> listFirst = root.elements(); 24 // 迭代 25 for (Element e : listFirst) { 26 // 擷取一級子項目的屬性值 27 String path = e.attributeValue("className"); 28 // 根據路徑(屬性)擷取位元組碼檔案 29 Class c = Class.forName(path); 30 // 擷取二級子項目 31 List<Element> listSecond = e.elements(); 32 // 定義map集合裝屬性值 33 Map<String, Object> map = new HashMap<>(); 34 for (Element es : listSecond) { 35 // 擷取二級子項目的兩個屬性值 36 String name = es.attributeValue("name"); 37 String value = es.attributeValue("value"); 38 map.put(name, value); 39 } 40 // 利用beanutils工具類進行封裝 41 // 判斷是否為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