Java JDK動態(tài)代理實現(xiàn)原理實例解析
JDK動態(tài)代理實現(xiàn)原理
動態(tài)代理機制
通過實現(xiàn) InvocationHandler 接口創(chuàng)建自己的調(diào)用處理器
通過為 Proxy 類指定 ClassLoader 對象和一組 interface 來創(chuàng)建動態(tài)代理類
通過反射機制獲得動態(tài)代理類的構(gòu)造函數(shù),其唯一參數(shù)類型是調(diào)用處理器接口類型
通過構(gòu)造函數(shù)創(chuàng)建動態(tài)代理類實例,構(gòu)造時調(diào)用處理器對象作為參數(shù)被傳入
Interface InvocationHandler
該接口中僅定義了一個方法Object:invoke(Object obj,Method method,Object[] args)。在實際使用時,第一個參數(shù)obj一般是指代理類,method是被代理的方法,args為該方法的參數(shù)數(shù)組。這個抽象方法在代理類中動態(tài)實現(xiàn)。
Proxy
該類即為動態(tài)代理類
Protected Proxy(InvocationHandler h)
構(gòu)造函數(shù),用于給內(nèi)部的h賦值
Static Class getProxyClass (ClassLoader loader,Class[] interfaces)
獲得一個代理類,其中l(wèi)oader是類裝載器,interfaces是真實類所擁有的全部接口的數(shù)組Static Object newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h)
返回代理類的一個實例,返回后的代理類可以當作被代理類使用(可使用被代理類的在Subject接口中聲明過的方法)
Dynamic Proxy
它是在運行時生成的class,在生成它時你必須提供一組interface給它,然后該class就宣稱它實現(xiàn)了這些 interface。你當然可以把該class的實例當作這些interface中的任何一個來用。當然啦,這個Dynamic Proxy其實就是一個Proxy,它不會替你作實質(zhì)性的工作,在生成它的實例時你必須提供一個handler,由它接管實際的工作。
代碼示例
創(chuàng)建接口:
/** * @CreateDate: 2019/6/17 14:52 * @Version: 1.0 */public interface BuyService { String buyPhone(); String buyComputer();}
創(chuàng)建實現(xiàn)類:
public class BuyServiceImpl implements BuyService { @Intercept('buyPhone') @Override public String buyPhone() { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('==========BuyServiceImpl.class=============' + ' buyPhone'); this.buyComputer(); return 'buy phone'; } @Intercept('buyComputer') @Override public String buyComputer() { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('==========BuyServiceImpl.class=============' + ' buyComputer'); return 'buy computer'; }}
創(chuàng)建 InvocationHandler:
public class ReflectionHandler implements InvocationHandler { private Object target; public ReflectionHandler(Object target) { this.target = target; } public <T> T getProxy(){ return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(target,args); }}
創(chuàng)建啟動類:
public class Bootstrap { public static void main(String[] args) { // 動態(tài)代理實現(xiàn) ReflectionHandler reflectionHandler = new ReflectionHandler(new BuyServiceImpl()); BuyService proxy = reflectionHandler.getProxy(); String computer = proxy.buyComputer(); String phone = proxy.buyPhone(); System.out.println(computer + 'rn' + phone); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Xml簡介_動力節(jié)點Java學院整理2. Python TestSuite生成測試報告過程解析3. Python查找算法之分塊查找算法的實現(xiàn)4. .NET 中配置從xml轉(zhuǎn)向json方法示例詳解5. 深入了解JAVA 軟引用6. python基于tkinter點擊按鈕實現(xiàn)圖片的切換7. python實現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例8. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題9. JSP之表單提交get和post的區(qū)別詳解及實例10. python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計處理操作
