Java web-內省(Introspector)__html5

來源:互聯網
上載者:User

這個反射類是在網易雲課堂上30天輕鬆掌握JavaWeb裡面學習的。可以寫架構用。但是為什麼還要學呢。當然是為了用嘍。
內省是sun公司開發的一套API,專門用於操作java對象的屬性,雖然反射技術也可以,但是反射技術過於麻煩。 內省訪問JavaBean屬性的兩種方式:
1:通過PrepertyDescriptor類操作Bean的屬性
2:通過Introspector類獲得Bean對象的BeanInfo,然後通過BeanInfo來擷取屬性描述器(PropertyDescriptor),通過這個屬性描述器就可以獲得某個屬性相應的getter/setter方法,在通過反射機制來調用這些方法 什麼是JavaBean

package com.test.introspector;public class Person { //JavaBean    private String name;    private String password;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }     public int getAB() {        return ab;    }    public void setAB(int age) {        this.ab = ab;    }}

如上面的Person類便是一個JavaBean,在java web中,使用者會提交許多資料,比如賬戶名,密碼等,而我們要建立一個對象去接收使用者的資料,建立的這個對象就是一個javaBean.
javabean的屬性取決於javabean中的getter/setter方法,而不是其定義的變數。
如上面代碼,其中並沒有定義ab變數,但是有getAB和setAB方法,則我們說這個javaBean中有ab屬性。
注意:上面的Person類一共有5個屬性(name, password, age, ab, class),class屬性是繼承其父類Object中的屬性。也就是我們建立的每一個javaBean中都會有一個class屬性。 寫內省類的一般步驟:
用Introspector類中的getBeanInfo得到BeanInfo對象 -> 利用BeanInfo對象中的的getPropertDesriptors()得到PropertDescriptors(屬性描述器)對象 ->
利用PropertDescriptor對象中的setReadMethod、setWriteMethod(JavaBean中的getter/setter方法)對JavaBean中的屬性進行操作。

package com.test.introspector;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import org.junit.Test;//使用內省api操作bean的屬性public class Dome1 {    @Test    public void test1() throws Exception {        BeanInfo info = Introspector.getBeanInfo(Person.class); //得到bean的所有屬性,包括繼承屬性//        BeanInfo info = Introspector.getBeanInfo(Person.class, Object.class);//得到bean自己的屬性,不包括繼承Object繼承的屬性        PropertyDescriptor[] pds = info.getPropertyDescriptors(); //建立PropertyDescriptor數組        for (PropertyDescriptor pd : pds) {            System.out.println(pd.getName());  //輸出JavaBean所有的屬性。        }    }    //操縱bean的指定屬性:age    @Test    public void test2() throws Exception {        Person p = new Person(); //只是為了省事,應該用reflect出Preson對象        PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);         //得到屬性的寫方法,為屬性賦值        Method method = pd.getWriteMethod(); //等同public void setAge(int age)        method.invoke(p, 666);        //擷取屬性的值//        System.out.println(p.getAge()); //一般方法        method = pd.getReadMethod();        System.out.println(method.invoke(p)); //age的get方法無參數,所以只傳一個Person對象    }    //擷取當前操作屬性的類型    @Test    public void test3() throws Exception {        PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);         System.out.println(pd.getPropertyType()); //getPropertyType返回的是class類型    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.