posts - 495,comments - 227,trackbacks - 0
          使用下邊的方法,或者轉(zhuǎn)成fastjson的JsonObject對(duì)象
          http://blog.csdn.net/cuidiwhere/article/details/8130434

          1. 為什么要實(shí)現(xiàn)javaBean與Map<String,Object>相互轉(zhuǎn)換?

          用過(guò)spring的都知道spring的MVC框架中有一個(gè)BaseCommandController對(duì)象,利用這個(gè)對(duì)象我們就可以很方便的將從 客戶端傳遞過(guò)來(lái)的參數(shù)封裝到一個(gè)JavaBean對(duì)象中去,而不需要我們 request.getParameter("name");bean.setName(name);了,從而也簡(jiǎn)化了不少的工作。如果大家用過(guò) BeanUtils.populate的話,就知道,這個(gè)方法是可以很方便的將request提交的頁(yè)面表單自動(dòng)填寫(xiě)到你創(chuàng)建的對(duì)象中

          2. 如何實(shí)現(xiàn)javaBean與Map<String,Object>相互轉(zhuǎn)換?

          方法1: 利用java.beans.Introspector和java.beans.PropertyDescriptor實(shí)現(xiàn) javaBean與Map<String,Object>互轉(zhuǎn)

          方法2: 利用org.apache.commons.beanutils.BeanUtils工具類(lèi),BeanUtils.populate實(shí)現(xiàn)Map 轉(zhuǎn)換為javaBean 


          1. package javaStudyDemo.bean.reflect.test;  
          2.   
          3. import java.beans.BeanInfo;  
          4. import java.beans.Introspector;  
          5. import java.beans.PropertyDescriptor;  
          6. import java.lang.reflect.Method;  
          7. import java.util.HashMap;  
          8. import java.util.Map;  
          9. import javaStudyDemo.others.PersonBean;  
          10.   
          11. import org.apache.commons.beanutils.BeanUtils;  
          12.   
          13. /** 
          14.  * 當(dāng)把Person類(lèi)作為BeanUtilTest的內(nèi)部類(lèi)時(shí),程序出錯(cuò)<br> 
          15.  * java.lang.NoSuchMethodException: Property '**' has no setter method<br> 
          16.  * 本質(zhì):內(nèi)部類(lèi) 和 單獨(dú)文件中的類(lèi)的區(qū)別 <br> 
          17.  * BeanUtils.populate方法的限制:<br> 
          18.  * The class must be public, and provide a public constructor that accepts no arguments. <br> 
          19.  * This allows tools and applications to dynamically create new instances of your bean, <br> 
          20.  * without necessarily knowing what Java class name will be used ahead of time 
          21.  */  
          22. public class BeanUtilTest {  
          23.   
          24.     public static void main(String[] args) {  
          25.   
          26.         PersonBean person = new PersonBean();  
          27.         Map<String, Object> mp = new HashMap<String, Object>();  
          28.         mp.put("name", "Mike");  
          29.         mp.put("age", 25);  
          30.         mp.put("mN", "male");  
          31.   
          32.         // 將map轉(zhuǎn)換為bean  
          33.         transMap2Bean2(mp, person);  
          34.   
          35.         System.out.println("--- transMap2Bean Map Info: ");  
          36.         for (Map.Entry<String, Object> entry : mp.entrySet()) {  
          37.             System.out.println(entry.getKey() + ": " + entry.getValue());  
          38.         }  
          39.   
          40.         System.out.println("--- Bean Info: ");  
          41.         System.out.println("name: " + person.getName());  
          42.         System.out.println("age: " + person.getAge());  
          43.         System.out.println("mN: " + person.getmN());  
          44.   
          45.         // 將javaBean 轉(zhuǎn)換為map  
          46.         Map<String, Object> map = transBean2Map(person);  
          47.   
          48.         System.out.println("--- transBean2Map Map Info: ");  
          49.         for (Map.Entry<String, Object> entry : map.entrySet()) {  
          50.             System.out.println(entry.getKey() + ": " + entry.getValue());  
          51.         }  
          52.   
          53.     }  
          54.   
          55.     // Map --> Bean 2: 利用org.apache.commons.beanutils 工具類(lèi)實(shí)現(xiàn) Map --> Bean  
          56.     public static void transMap2Bean2(Map<String, Object> map, Object obj) {  
          57.         if (map == null || obj == null) {  
          58.             return;  
          59.         }  
          60.         try {  
          61.             BeanUtils.populate(obj, map);  
          62.         } catch (Exception e) {  
          63.             System.out.println("transMap2Bean2 Error " + e);  
          64.         }  
          65.     }  
          66.   
          67.     // Map --> Bean 1: 利用Introspector,PropertyDescriptor實(shí)現(xiàn) Map --> Bean  
          68.     public static void transMap2Bean(Map<String, Object> map, Object obj) {  
          69.   
          70.         try {  
          71.             BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());  
          72.             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
          73.   
          74.             for (PropertyDescriptor property : propertyDescriptors) {  
          75.                 String key = property.getName();  
          76.   
          77.                 if (map.containsKey(key)) {  
          78.                     Object value = map.get(key);  
          79.                     // 得到property對(duì)應(yīng)的setter方法  
          80.                     Method setter = property.getWriteMethod();  
          81.                     setter.invoke(obj, value);  
          82.                 }  
          83.   
          84.             }  
          85.   
          86.         } catch (Exception e) {  
          87.             System.out.println("transMap2Bean Error " + e);  
          88.         }  
          89.   
          90.         return;  
          91.   
          92.     }  
          93.   
          94.     // Bean --> Map 1: 利用Introspector和PropertyDescriptor 將Bean --> Map  
          95.     public static Map<String, Object> transBean2Map(Object obj) {  
          96.   
          97.         if(obj == null){  
          98.             return null;  
          99.         }          
          100.         Map<String, Object> map = new HashMap<String, Object>();  
          101.         try {  
          102.             BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());  
          103.             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
          104.             for (PropertyDescriptor property : propertyDescriptors) {  
          105.                 String key = property.getName();  
          106.   
          107.                 // 過(guò)濾class屬性  
          108.                 if (!key.equals("class")) {  
          109.                     // 得到property對(duì)應(yīng)的getter方法  
          110.                     Method getter = property.getReadMethod();  
          111.                     Object value = getter.invoke(obj);  
          112.   
          113.                     map.put(key, value);  
          114.                 }  
          115.   
          116.             }  
          117.         } catch (Exception e) {  
          118.             System.out.println("transBean2Map Error " + e);  
          119.         }  
          120.   
          121.         return map;  
          122.   
          123.     }  
          124. }  


          1. public class PersonBean {  
          2.   
          3.     private String  name;  
          4.     private Integer age;  
          5.     private String  mN;  
          6.   
          7.     /** 
          8.      * @return the mN 
          9.      */  
          10.     public String getmN() {  
          11.         return mN;  
          12.     }  
          13.   
          14.     /** 
          15.      * @param mN the mN to set 
          16.      */  
          17.     public void setmN(String mN) {  
          18.         this.mN = mN;  
          19.     }  
          20.   
          21.   
          22.     /** 
          23.      * @return the name 
          24.      */  
          25.     public String getName() {  
          26.         return name;  
          27.     }  
          28.   
          29.     /** 
          30.      * @param name the name to set 
          31.      */  
          32.     public void setName(String name) {  
          33.         this.name = name;  
          34.     }  
          35.   
          36.     /** 
          37.      * @return the age 
          38.      */  
          39.     public Integer getAge() {  
          40.         return age;  
          41.     }  
          42.   
          43.     /** 
          44.      * @param age the age to set 
          45.      */  
          46.     public void setAge(Integer age) {  
          47.         this.age = age;  
          48.     }  
          49.   
          50. }  


          總結(jié):  javaBean與Map<String,Object>互轉(zhuǎn)利用到了java的內(nèi)省( Introspector )和反射(reflect)機(jī)制。 其思路為: 通 過(guò)類(lèi) Introspector 來(lái)獲取某個(gè)對(duì)象的 BeanInfo 信息,然后通過(guò) BeanInfo 來(lái)獲取屬性的描述器 PropertyDescriptor,再利用屬性描述器獲取某個(gè)屬性對(duì)應(yīng)的 getter/setter 方法,然后通過(guò)反射機(jī)制來(lái)getter和 setter。


          什么是內(nèi)省? 

          內(nèi) 省是 Java 語(yǔ)言對(duì) Bean 類(lèi)屬性、事件的一種缺省處理方法。例如類(lèi) PersonBean中有屬性 name, 那我們可以通 過(guò) getName,setName 來(lái)得到其值或者設(shè)置新的值。通過(guò) getName/setName 來(lái)訪問(wèn) name 屬性,這就是默認(rèn)的規(guī) 則。 Java 中提供了一套 API 用來(lái)訪問(wèn)某個(gè)屬性的 getter/setter 方法,通過(guò)這些 API 可以使你不需要了解這個(gè)規(guī)則(但你最 好還是要搞清楚),這些 API 存放于包 java.beans 中。注意: PersonBean中屬性mN的getter/setter方法必須滿足javaBean命名規(guī)范,即getmN,不能寫(xiě)作getMN,否則轉(zhuǎn)換失敗。 詳情參考  http://blog.renren.com/share/236384819/5598710664

           

          posted on 2015-06-30 18:44 SIMONE 閱讀(846) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA
          主站蜘蛛池模板: 永清县| 巴林左旗| 玉龙| 新巴尔虎右旗| 墨竹工卡县| 阿鲁科尔沁旗| 鄂托克前旗| 南华县| 兰考县| 太白县| 娄烦县| 固安县| 汤原县| 宁海县| 博爱县| 驻马店市| 通山县| 瓦房店市| 普陀区| 嘉兴市| 宝坻区| 高州市| 田阳县| 乐清市| 门源| 永德县| 万安县| 五家渠市| 名山县| 阿鲁科尔沁旗| 收藏| 信阳市| 托克逊县| 石首市| 南充市| 夏河县| 吉林市| 池州市| 即墨市| 井冈山市| 汝州市|