Java內(nèi)省實(shí)例解析
圖像中輪廓的個(gè)數(shù),里面vector的size代表了輪廓上點(diǎn)的個(gè)數(shù)。了解JavaBean
內(nèi)省對(duì)應(yīng)的英文單詞為IntroSpector,它主要用于對(duì)JavaBean進(jìn)行操作,JavaBean是一種特殊的Java類(lèi),其中的某些方法符合某種命名規(guī)則,如果一個(gè)Java類(lèi)中的一些方法符合某種命名規(guī)則,則可以把它當(dāng)作JavaBean來(lái)使用。
JavaBean是一種特殊的Java類(lèi),主要用于傳遞數(shù)據(jù)信息,這種java類(lèi)中的方法主要用于訪(fǎng)問(wèn)私有的字段,且方法名符合某種命名規(guī)則。
如果要在兩個(gè)模塊之間傳遞多個(gè)信息,可以將這些信息封裝到一個(gè)JavaBean中,這種JavaBean的實(shí)例對(duì)象通常稱(chēng)之為值對(duì)象(ValueObject,簡(jiǎn)稱(chēng)VO)。這些信息在類(lèi)中用私有字段來(lái)存儲(chǔ),如果讀取或設(shè)置這些字段的值,則需要通過(guò)一些相應(yīng)的方法來(lái)訪(fǎng)問(wèn),大家覺(jué)得這些方法的名稱(chēng)叫什么好呢?JavaBean的屬性是根據(jù)其中的setter和getter方法來(lái)確定的,而不是根據(jù)其中的成員變量。如果方法名為setId,中文意思即為設(shè)置id,至于你把它存到哪個(gè)變量上,用管嗎?如果方法名為getId,中文意思即為獲取id,至于你從哪個(gè)變量上取,用管嗎?去掉set前綴,剩余部分就是屬性名,如果剩余部分的第二個(gè)字母是小寫(xiě)的,則把剩余部分的首字母改成小的。
例如:
setId()的屬性名-->id
isLast()的屬性名-->last
setCPU的屬性名是什么?-->CPU
getUPS的屬性名是什么?-->UPS
總之,一個(gè)類(lèi)被當(dāng)作javaBean使用時(shí),JavaBean的屬性是根據(jù)方法名推斷出來(lái)的,它根本看不到j(luò)ava類(lèi)內(nèi)部的成員變量。
一個(gè)符合JavaBean特點(diǎn)的類(lèi)可以當(dāng)作普通類(lèi)一樣進(jìn)行使用,但把它當(dāng)JavaBean用肯定需要帶來(lái)一些額外的好處,我們才會(huì)去了解和應(yīng)用JavaBean!好處如下:
在JavaEE開(kāi)發(fā)中,經(jīng)常要使用到JavaBean。很多環(huán)境就要求按JavaBean方式進(jìn)行操作,別人都這么用和要求這么做,那你就沒(méi)什么挑選的余地!
JDK中提供了對(duì)JavaBean進(jìn)行操作的一些API,這套API就稱(chēng)為內(nèi)省。如果要你自己去通過(guò)getX方法來(lái)訪(fǎng)問(wèn)私有的x,怎么做,有一定難度吧?用內(nèi)省這套api操作JavaBean比用普通類(lèi)的方式更方便。
對(duì)JavaBean的簡(jiǎn)單內(nèi)省操作
主要用到了java.beans.PropertyDescriptor類(lèi),用來(lái)得到某個(gè)Class對(duì)象屬性集中的某個(gè)JavaBean屬性,然后調(diào)用getReadMethod()、getWriteMethod()方法獲得相應(yīng)的get、set方法。
代碼示例:
Domain類(lèi):
[cpp]viewplaincopy
intmain()
package ustc.lichunchun.bean; import java.util.Date; public class ReflectPoint { private Date birthday = new Date(); private int x; public int y; public String str1 = "ball"; public String str2 = "basketball"; public String str3 = "itcast"; public ReflectPoint(int x, int y) { super(); this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public Boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final ReflectPoint other = (ReflectPoint) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } @Override public String toString(){ return str1 + ":" + str2 + ":" + str3; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
簡(jiǎn)單內(nèi)省操作:
package ustc.lichunchun.bean; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class IntroSpectorTest { public static void main(String[] args) throws Exception { ReflectPoint pt1 = new ReflectPoint(3, 5); String propertyName = "x"; //"x"-->"X"-->"getX"-->MethodGetX--> getProperty(pt1, propertyName); Object value = 7; setProperty(pt1, propertyName, value); System.out.println(pt1.getX()); } private static void setProperty(Object pt1, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodSetX = pd.getWriteMethod(); methodSetX.invoke(pt1, value); } private static Object getProperty(Object pt1, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodGetX = pd.getReadMethod(); methodGetX.invoke(pt1); } }
對(duì)JavaBean的復(fù)雜內(nèi)省操作
采用遍歷BeanInfo的所有屬性方式來(lái)查找和設(shè)置某個(gè)RefectPoint對(duì)象的x屬性。在程序中把一個(gè)類(lèi)當(dāng)作JavaBean來(lái)看,就是調(diào)用IntroSpector.getBeanInfo方法,得到的BeanInfo對(duì)象封裝了把這個(gè)類(lèi)當(dāng)作JavaBean看的結(jié)果信息。
復(fù)雜內(nèi)省操作:
package ustc.lichunchun.bean; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class IntroSpectorTest { public static void main(String[] args) throws Exception { ReflectPoint pt1 = new ReflectPoint(3, 5); String propertyName = "x"; //"x"-->"X"-->"getX"-->MethodGetX--> Object retVal = getProperty(pt1, propertyName); System.out.println(retVal); Object value = 7; setProperty(pt1, propertyName, value); System.out.println(pt1.getX()); } private static void setProperty(Object pt1, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodSetX = pd.getWriteMethod(); methodSetX.invoke(pt1, value); } private static Object getProperty(Object pt1, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException { /* PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodGetX = pd.getReadMethod(); methodGetX.invoke(pt1); */ BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass()); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); Object retVal = null; for (PropertyDescriptor pd : pds){ if(pd.getName().equals(propertyName)){ Method methodGetX = pd.getReadMethod(); retVal = methodGetX.invoke(pt1); break; } } return retVal; } }
使用BeanUtils工具包操作JavaBean
在前面內(nèi)省例子的基礎(chǔ)上,用BeanUtils類(lèi)先get原來(lái)設(shè)置好的屬性,再將其set為一個(gè)新值。get屬性時(shí)返回的結(jié)果為字符串,set屬性時(shí)可以接受任意類(lèi)型的對(duì)象,通常使用字符串。
用PropertyUtils類(lèi)先get原來(lái)設(shè)置好的屬性,再將其set為一個(gè)新值。get屬性時(shí)返回的結(jié)果為該屬性本來(lái)的類(lèi)型,set屬性時(shí)只接受該屬性本來(lái)的類(lèi)型。
注意:用這兩個(gè)類(lèi)之前,需要在eclipse工程的lib文件夾中導(dǎo)入commons-beanutils.jar、commons-logging-1.1.jar兩個(gè)jar包,并且AddtoBuildPath。
代碼示例:
package ustc.lichunchun.bean; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; public class IntroSpectorTest { public static void main(String[] args) throws Exception { ReflectPoint pt1 = new ReflectPoint(3, 5); String propertyName = "x"; //"x"-->"X"-->"getX"-->MethodGetX--> Object retVal = getProperty(pt1, propertyName); System.out.println(retVal); Object value = 7; setProperty(pt1, propertyName, value); System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName()); //String BeanUtils.setProperty(pt1, "x", "9"); System.out.println(pt1.getX()); /* Map map = {name:"zxx",age:18};//java7的新特性 BeanUtils.setProperty(map, "name", "lcc"); */ BeanUtils.setProperty(pt1, "birthday.time", "111"); //支持屬性鏈 System.out.println(BeanUtils.getProperty(pt1, "birthday.time")); PropertyUtils.setProperty(pt1, "x", 23); System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName()); //Integer /* BeanUtils和PropertyUtils的區(qū)別: BeanUtils以字符串形式對(duì)JavaBean進(jìn)行操作,也可以操作Map類(lèi),并且可以講JavaBean和Map進(jìn)行互相轉(zhuǎn)換(describe、populate) PropertyUtils以JavaBean屬性本身的數(shù)據(jù)類(lèi)型進(jìn)行操作 */ } private static void setProperty(Object pt1, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodSetX = pd.getWriteMethod(); methodSetX.invoke(pt1, value); } private static Object getProperty(Object pt1, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException { /* PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodGetX = pd.getReadMethod(); methodGetX.invoke(pt1); */ BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass()); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); Object retVal = null; for (PropertyDescriptor pd : pds){ if(pd.getName().equals(propertyName)){ Method methodGetX = pd.getReadMethod(); retVal = methodGetX.invoke(pt1); break; } } return retVal; } }
總結(jié)
以上就是本文關(guān)于Java內(nèi)省實(shí)例解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a
MyBatis-Flex它提供了一種鏈?zhǔn)讲僮鞣绞?本文主要介紹了mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06淺談DetachedCriteria和Criteria的使用方法(必看)
下面小編就為大家?guī)?lái)一篇淺談DetachedCriteria和Criteria的使用方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Java中關(guān)鍵字final finally finalize的區(qū)別介紹
這篇文章主要給大家分享的是 Java中final,finally,finalize 到底有什么區(qū)別,文章圍繞final,finally,finalize的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,具有一定的參考的價(jià)值,需要的朋友可以參考一下2022-04-04Java加載本地庫(kù)的方法之System.load與System.loadLibrary
最近在做的工作要用到本地方法,所以下面這篇文章主要介紹了Java加載本地庫(kù)的方法之System.load與System.loadLibrary的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09SpringBoot+Mybatis實(shí)現(xiàn)登錄注冊(cè)的示例代碼
這篇文章主要介紹了SpringBoot+Mybatis實(shí)現(xiàn)登錄注冊(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03@Transactional跟@DS動(dòng)態(tài)數(shù)據(jù)源注解沖突的解決
這篇文章主要介紹了@Transactional跟@DS動(dòng)態(tài)數(shù)據(jù)源注解沖突的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java結(jié)構(gòu)型設(shè)計(jì)模式之裝飾模式詳解
裝飾模式(Decorator Pattern)允許向一個(gè)現(xiàn)有的對(duì)象添加新的功能,同時(shí)又不改變其結(jié)構(gòu)。這種類(lèi)型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有類(lèi)的一個(gè)包裝。這種模式創(chuàng)建了一個(gè)裝飾類(lèi),用來(lái)包裝原有的類(lèi),并在保持類(lèi)方法簽名完整性的前提下,提供了額外的功能2023-03-03