Introspector内省和反射的区别.
生活随笔
收集整理的這篇文章主要介紹了
Introspector内省和反射的区别.
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Introspector?是一個專門處理bean的工具類.用來獲取Bean體系里的 propertiesDescriptor,methodDescriptor.
要理解這個,就要理解下面幾個議題.
*bean是啥?普通的class 可能有 computerAges(){ }等方法. Bean是 一個field ,有?get 或者set. 除了這些別無其他. bean是class的一種 例如 public class People { String name; public String getName(){ } public void setName(String name){ } } *Bean在jdk里對應(yīng)的的概念 BeanInfo?, 他包含了Bean所有的descriptor(描述符) . BeanDescriptor PropertiesDescriptor MethodDescriptor * ?一個類的屬性field 和 propertiesDescriptor(描述)有什么區(qū)別.
propertiesDescriptor,它來至于 對Method的解析. 如果是嚴(yán)格的Bean.例如上面的People. field一個叫做name,?propertiesDescriptor 只有一個,剛好也是name, 來自set和get的解析, 解析出來都是? ? ? ?name.,所有兩個merge為一個.? 詳細(xì)邏輯見Introspector中代碼.見附件 * ?反射的method和bean概念體系里的methodDescriptor的區(qū)別 2:1的對應(yīng)關(guān)系. People里有set和get兩個方法,反射得到兩個Method,但這兩個method會組合成一個MethodDescriptor.? * ?Introspector內(nèi)省 和 反射的區(qū)別和關(guān)系? Introspector?是一個專門處理bean的工具類.用來獲取Bean體系里的 propertiesDescriptor,methodDescriptor. 利用反射獲取Method信息,是反射的上層.? 性能優(yōu)化:?只進(jìn)行一次反射解析. 通過WeakReference靜態(tài)類級別緩存Method, 在jvm不夠時會被回收.?? //?Static?Caches?to?speed?up?introspection. private?static?Map?declaredMethodCache?=?Collections.synchronizedMap(new?WeakHashMap()); 附件1: 解析method得到properties,并且合并同名的properties. 把 method根據(jù) 解析出的properties放入的map中,將 setMethod和 getMethod合并成一個 methodDescriptor 見 Introspector.java的
/** *?Populates?the?property?descriptor?table?by?merging?the *?lists?of?Property?descriptors. */ ????private?void?processPropertyDescriptors()?{? ... //?Complete?simple?properties?set? pd?=?mergePropertyDescriptor(gpd,?spd);?//merge get方法解析出的gpd和set方法解析出的spd . 一個PropertyDescriptor里面有兩個屬性,一個是setMethodName,一個是getMethodName.
? ? ? ? ? ? ? ? ? .... properties.put(pd.getName(),?pd);?? }? /** *?Adds?the?property?descriptor?to?the?indexedproperty?descriptor?only?if?the *?types?are?the?same. * *?The?most?specific?property?descriptor?will?take?precedence. */ private?PropertyDescriptor?mergePropertyDescriptor(IndexedPropertyDescriptor?ipd, ???????????????????????????????????????????????????????PropertyDescriptor?pd)?{?? } PropertyDescriptor里的?private?Reference<Class>?propertyTypeRef;?里的值決定了type, 距離, int string等類型.
轉(zhuǎn)載于:https://www.cnblogs.com/zjj1996/p/9139700.html
總結(jié)
以上是生活随笔為你收集整理的Introspector内省和反射的区别.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js正则表达式匹配span标签
- 下一篇: 302重定向问题