Java Proxy機(jī)制詳細(xì)解讀
動(dòng)態(tài)代理其實(shí)就是java.lang.reflect.Proxy類動(dòng)態(tài)的根據(jù)您指定的所有接口生成一個(gè)class byte,該class會(huì)繼承Proxy類,并實(shí)現(xiàn)所有你指定的接口(您在參數(shù)中傳入的接口數(shù)組);然后再利用您指定的classloader將 class byte加載進(jìn)系統(tǒng),最后生成這樣一個(gè)類的對(duì)象,并初始化該對(duì)象的一些值,如invocationHandler,以即所有的接口對(duì)應(yīng)的Method成員。 初始化之后將對(duì)象返回給調(diào)用的客戶端。這樣客戶端拿到的就是一個(gè)實(shí)現(xiàn)你所有的接口的Proxy對(duì)象。請(qǐng)看實(shí)例分析:
一 業(yè)務(wù)接口類
public interface BusinessProcessor { public void processBusiness(); }
二 業(yè)務(wù)實(shí)現(xiàn)類
public class BusinessProcessorImpl implements BusinessProcessor { public void processBusiness() { System.out.println("processing business....."); } }
三 業(yè)務(wù)代理類
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class BusinessProcessorHandler implements InvocationHandler { private Object target = null; BusinessProcessorHandler(Object target){ this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("You can do something here before process your business"); Object result = method.invoke(target, args); System.out.println("You can do something here after process your business"); return result; } }
四 客戶端應(yīng)用類
import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; public class Test { public static void main(String[] args) { BusinessProcessorImpl bpimpl = new BusinessProcessorImpl(); BusinessProcessorHandler handler = new BusinessProcessorHandler(bpimpl); BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(bpimpl.getClass().getClassLoader(), bpimpl.getClass().getInterfaces(), handler); bp.processBusiness(); } }
現(xiàn)在我們看一下打印結(jié)果:
You can do something here before process your business processing business..... You can do something here after process your business
通過結(jié)果我們就能夠很簡(jiǎn)單的看出Proxy的作用了,它能夠在你的核心業(yè)務(wù)方法前后做一些你所想做的輔助工作,如log日志,安全機(jī)制等等。
現(xiàn)在我們來分析一下上面的類的工作原理。
類一二沒什么好說的。先看看類三吧。 實(shí)現(xiàn)了InvocationHandler接口的invoke方法。其實(shí)這個(gè)類就是最終Proxy調(diào)用的固定接口方法。Proxy不管客戶端的業(yè)務(wù)方法是怎么實(shí)現(xiàn)的。當(dāng)客戶端調(diào)用Proxy時(shí),它只會(huì)調(diào)用InvocationHandler的invoke接口,所以我們的真正實(shí)現(xiàn)的方法就必須在invoke方法中去調(diào)用。關(guān)系如下:
BusinessProcessorImpl bpimpl = new BusinessProcessorImpl(); BusinessProcessorHandler handler = new BusinessProcessorHandler(bpimpl); BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(....); bp.processBusiness()-->invocationHandler.invoke()-->bpimpl.processBusiness();
那么bp到底是怎么樣一個(gè)對(duì)象呢。我們改一下main方法看一下就知道了:
public static void main(String[] args) { BusinessProcessorImpl bpimpl = new BusinessProcessorImpl(); BusinessProcessorHandler handler = new BusinessProcessorHandler(bpimpl); BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(bpimpl.getClass().getClassLoader(), bpimpl.getClass().getInterfaces(), handler); bp.processBusiness(); System.out.println(bp.getClass().getName()); }
輸出結(jié)果:
You can do something here before process your business processing business..... You can do something here after process your business $Proxy0
bp原來是個(gè)$Proxy0這個(gè)類的對(duì)象。那么這個(gè)類到底是長(zhǎng)什么樣子呢?好的。我們?cè)賹懚€(gè)方法去把這個(gè)類打印出來看個(gè)究竟,是什么三頭六臂呢?我們?cè)趍ain下面寫如下兩個(gè)靜態(tài)方法。
public static String getModifier(int modifier){ String result = ""; switch(modifier){ case Modifier.PRIVATE: result = "private"; case Modifier.PUBLIC: result = "public"; case Modifier.PROTECTED: result = "protected"; case Modifier.ABSTRACT : result = "abstract"; case Modifier.FINAL : result = "final"; case Modifier.NATIVE : result = "native"; case Modifier.STATIC : result = "static"; case Modifier.SYNCHRONIZED : result = "synchronized"; case Modifier.STRICT : result = "strict"; case Modifier.TRANSIENT : result = "transient"; case Modifier.VOLATILE : result = "volatile"; case Modifier.INTERFACE : result = "interface"; } return result; } public static void printClassDefinition(Class clz){ String clzModifier = getModifier(clz.getModifiers()); if(clzModifier!=null && !clzModifier.equals("")){ clzModifier = clzModifier + " "; } String superClz = clz.getSuperclass().getName(); if(superClz!=null && !superClz.equals("")){ superClz = "extends " + superClz; } Class[] interfaces = clz.getInterfaces(); String inters = ""; for(int i=0; i<interfaces.length; i++){ if(i==0){ inters += "implements "; } inters += interfaces[i].getName(); } System.out.println(clzModifier +clz.getName()+" " + superClz +" " + inters ); System.out.println("{"); Field[] fields = clz.getDeclaredFields(); for(int i=0; i<fields.length; i++){ String modifier = getModifier(fields[i].getModifiers()); if(modifier!=null && !modifier.equals("")){ modifier = modifier + " "; } String fieldName = fields[i].getName(); String fieldType = fields[i].getType().getName(); System.out.println(" "+modifier + fieldType + " "+ fieldName + ";"); } System.out.println(); Method[] methods = clz.getDeclaredMethods(); for(int i=0; i<methods.length; i++){ Method method = methods[i]; String modifier = getModifier(method.getModifiers()); if(modifier!=null && !modifier.equals("")){ modifier = modifier + " "; } String methodName = method.getName(); Class returnClz = method.getReturnType(); String retrunType = returnClz.getName(); Class[] clzs = method.getParameterTypes(); String paraList = "("; for(int j=0; j<clzs.length; j++){ paraList += clzs[j].getName(); if(j != clzs.length -1 ){ paraList += ", "; } } paraList += ")"; clzs = method.getExceptionTypes(); String exceptions = ""; for(int j=0; j<clzs.length; j++){ if(j==0){ exceptions += "throws "; } exceptions += clzs[j].getName(); if(j != clzs.length -1 ){ exceptions += ", "; } } exceptions += ";"; String methodPrototype = modifier +retrunType+" "+methodName+paraList+exceptions; System.out.println(" "+methodPrototype ); } System.out.println("}"); }
再改寫main方法
public static void main(String[] args) { BusinessProcessorImpl bpimpl = new BusinessProcessorImpl(); BusinessProcessorHandler handler = new BusinessProcessorHandler(bpimpl); BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(bpimpl.getClass().getClassLoader(), bpimpl.getClass().getInterfaces(), handler); bp.processBusiness(); System.out.println(bp.getClass().getName()); Class clz = bp.getClass(); printClassDefinition(clz); }
現(xiàn)在我們?cè)倏纯摧敵鼋Y(jié)果:
You can do something here before process your business processing business..... You can do something here after process your business $Proxy0 $Proxy0 extends java.lang.reflect.Proxy implements com.tom.proxy.dynamic.BusinessProcessor { java.lang.reflect.Method m4; java.lang.reflect.Method m2; java.lang.reflect.Method m0; java.lang.reflect.Method m3; java.lang.reflect.Method m1; void processBusiness(); int hashCode(); boolean equals(java.lang.Object); java.lang.String toString(); }
很明顯,Proxy.newProxyInstance方法會(huì)做如下幾件事:
1,根據(jù)傳入的第二個(gè)參數(shù)interfaces動(dòng)態(tài)生成一個(gè)類,實(shí)現(xiàn)interfaces中的接口,該例中即BusinessProcessor接口的processBusiness方法。并且繼承了Proxy類,重寫了hashcode,toString,equals等三個(gè)方法。具體實(shí)現(xiàn)可參看 ProxyGenerator.generateProxyClass(...); 該例中生成了$Proxy0類
2,通過傳入的第一個(gè)參數(shù)classloder將剛生成的類加載到j(luò)vm中。即將$Proxy0類load
3,利用第三個(gè)參數(shù),調(diào)用$Proxy0的$Proxy0(InvocationHandler)構(gòu)造函數(shù) 創(chuàng)建$Proxy0的對(duì)象,并且用interfaces參數(shù)遍歷其所有接口的方法,并生成Method對(duì)象初始化對(duì)象的幾個(gè)Method成員變量
4,將$Proxy0的實(shí)例返回給客戶端。
現(xiàn)在好了。我們?cè)倏纯蛻舳嗽趺凑{(diào)就清楚了。
1,客戶端拿到的是$Proxy0的實(shí)例對(duì)象,由于$Proxy0繼承了BusinessProcessor,因此轉(zhuǎn)化為BusinessProcessor沒任何問題。
BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(....);
2,bp.processBusiness();
實(shí)際上調(diào)用的是$Proxy0.processBusiness();那么$Proxy0.processBusiness()的實(shí)現(xiàn)就是通過InvocationHandler去調(diào)用invoke方法啦!
總結(jié)
以上就是本文關(guān)于Java Proxy機(jī)制詳細(xì)解讀的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:詳解java中的互斥鎖信號(hào)量和多線程等待機(jī)制、關(guān)于Java反射機(jī)制 你需要知道的事情、Java的RTTI和反射機(jī)制代碼分析等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家。
相關(guān)文章
解決IDEA中Maven下載依賴包過慢或報(bào)錯(cuò)的問題
由于公司項(xiàng)目迭代,越來越多的項(xiàng)目開始轉(zhuǎn)型新版本,由于我對(duì)Java一直不感冒,但要順應(yīng)公司項(xiàng)目要求,遂自己要逐步開始完善Java相關(guān)的知識(shí)層面,此篇是我在學(xué)習(xí)SpringBoot時(shí)對(duì)一些不懂地方及遇到問題時(shí)的記錄,需要的朋友可以參考下2024-02-02Java中調(diào)用SQL Server存儲(chǔ)過程詳解
這篇文章主要介紹了Java中調(diào)用SQL Server存儲(chǔ)過程詳解,本文講解了使用不帶參數(shù)的存儲(chǔ)過程、使用帶有輸入?yún)?shù)的存儲(chǔ)過程、使用帶有輸出參數(shù)的存儲(chǔ)過程、使用帶有返回狀態(tài)的存儲(chǔ)過程、使用帶有更新計(jì)數(shù)的存儲(chǔ)過程等操作實(shí)例,需要的朋友可以參考下2015-01-01Java流程控制之循環(huán)結(jié)構(gòu)for,增強(qiáng)for循環(huán)
這篇文章主要介紹了Java流程控制之循環(huán)結(jié)構(gòu)for,增強(qiáng)for循環(huán),for循環(huán)是編程語言中一種循環(huán)語句,而循環(huán)語句由循環(huán)體及循環(huán)的判定條件兩部分組成,其表達(dá)式為:for(單次表達(dá)式;條件表達(dá)式;末尾循環(huán)體){中間循環(huán)體;},下面我們倆看看文章內(nèi)容的詳細(xì)介紹2021-12-12springboot項(xiàng)目訪問圖片的3種實(shí)現(xiàn)方法(親測(cè)可用)
本文主要介紹了springboot項(xiàng)目訪問圖片的3種實(shí)現(xiàn)方法,通過springboot項(xiàng)目訪問除項(xiàng)目根目錄之外的其它目錄的圖片,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09eclipse中自動(dòng)生成構(gòu)造函數(shù)的兩種方法
下面小編就為大家?guī)硪黄猠clipse中自動(dòng)生成構(gòu)造函數(shù)的兩種方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10