Java讀書筆記十一(Java中的內省機制)

來源:互聯網
上載者:User

標籤:


 1.前言

最近在學習Java的時候,突然發現了這個概念,鑒於好奇心的壓迫,於是打算寫一篇部落格來總結一下。


 2.什麼是內省機制

為了讓程式員們更好的從左Java對象的屬性,SUN公司開發了一套API,就被我們稱為“內省”,有利於我們對類對象的屬性的操作,減少了代碼的數量。


 3.內省和反射有什麼區別

反射式在運行狀態把Java類中的各種成分映射成相應的Java類,可以動態擷取所有的屬性以及動態調用任意一個方法,強調的是運行狀態。

內省機制是通過反射來實現的,BeanInfo用來暴露一個bean的屬性、方法和事件,以後我們就可以操縱該JavaBean的屬性


 4.一張圖總結



在Java內省中,用到的基本上就是上述幾個類。

通過BeanInfo這個類就可以擷取到類中的方法和屬性,具體的可以參考JDK文檔


 5.Demo展示

5.1.通過PropertyDescriptor修改屬性方式

package com.query;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;public class BeanInfoUtil {public static void setProperty(UserInfo userInfo,String userName)throws Exception{        PropertyDescriptor propDesc=new PropertyDescriptor(userName,UserInfo.class);        Method methodSetUserName=propDesc.getWriteMethod();        methodSetUserName.invoke(userInfo, "wong");        System.out.println("set userName:"+userInfo.getUserName());    }      public static void getProperty(UserInfo userInfo,String userName)throws Exception{        PropertyDescriptor proDescriptor =new PropertyDescriptor(userName,UserInfo.class);        Method methodGetUserName=proDescriptor.getReadMethod();        Object objUserName=methodGetUserName.invoke(userInfo);        System.out.println("get userName:"+objUserName.toString());    }}

5.2.通過Introspector類修改屬性

package com.query;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;public class BeanInfoUtil2 {public static void setPropertyByIntrospector(UserInfo userInfo,String userName) throws Exception {BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();if (proDescrtptors != null && proDescrtptors.length > 0) {for (PropertyDescriptor propDesc : proDescrtptors) {if (propDesc.getName().equals(userName)) {Method methodSetUserName = propDesc.getWriteMethod();methodSetUserName.invoke(userInfo, "alan");System.out.println("set userName:" + userInfo.getUserName());break;}}}}public static void getPropertyByIntrospector(UserInfo userInfo,String userName) throws Exception {BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();if (proDescrtptors != null && proDescrtptors.length > 0) {for (PropertyDescriptor propDesc : proDescrtptors) {if (propDesc.getName().equals(userName)) {Method methodGetUserName = propDesc.getReadMethod();Object objUserName = methodGetUserName.invoke(userInfo);System.out.println("get userName:" + objUserName.toString());break;}}}}}


注意事項,在上述修改JavaBean屬性的時候,如果資料類型不對的話,會報錯。例如BeanInfoUtil.setProperty(userInfo, "age");報錯是應為age屬性是int資料類型,而setProperty方法裡面預設給age屬性賦的值是String類型。所以會爆出argument type mismatch參數類型不符的錯誤資訊。

為瞭解決上述問題,Apache開發了一套簡單、易用的API來操作Bean的屬性——BeanUtils工具包。
  BeanUtils工具包:下載:http://commons.apache.org/beanutils/ 注意:應用的時候還需要一個logging包http://commons.apache.org/logging/









著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java讀書筆記十一(Java中的內省機制)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.