Java JDK動(dòng)態(tài)代理實(shí)現(xiàn)原理實(shí)例解析
JDK動(dòng)態(tài)代理實(shí)現(xiàn)原理
動(dòng)態(tài)代理機(jī)制
通過實(shí)現(xiàn) InvocationHandler 接口創(chuàng)建自己的調(diào)用處理器
通過為 Proxy 類指定 ClassLoader 對(duì)象和一組 interface 來創(chuàng)建動(dòng)態(tài)代理類
通過反射機(jī)制獲得動(dòng)態(tài)代理類的構(gòu)造函數(shù),其唯一參數(shù)類型是調(diào)用處理器接口類型
通過構(gòu)造函數(shù)創(chuàng)建動(dòng)態(tài)代理類實(shí)例,構(gòu)造時(shí)調(diào)用處理器對(duì)象作為參數(shù)被傳入
Interface InvocationHandler
該接口中僅定義了一個(gè)方法Object:invoke(Object obj,Method method,Object[] args)。在實(shí)際使用時(shí),第一個(gè)參數(shù)obj一般是指代理類,method是被代理的方法,args為該方法的參數(shù)數(shù)組。這個(gè)抽象方法在代理類中動(dòng)態(tài)實(shí)現(xiàn)。
Proxy
該類即為動(dòng)態(tài)代理類
Protected Proxy(InvocationHandler h)
構(gòu)造函數(shù),用于給內(nèi)部的h賦值
Static Class getProxyClass (ClassLoader loader,Class[] interfaces)
獲得一個(gè)代理類,其中l(wèi)oader是類裝載器,interfaces是真實(shí)類所擁有的全部接口的數(shù)組
Static Object newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h)
返回代理類的一個(gè)實(shí)例,返回后的代理類可以當(dāng)作被代理類使用(可使用被代理類的在Subject接口中聲明過的方法)
Dynamic Proxy
它是在運(yùn)行時(shí)生成的class,在生成它時(shí)你必須提供一組interface給它,然后該class就宣稱它實(shí)現(xiàn)了這些 interface。你當(dāng)然可以把該class的實(shí)例當(dāng)作這些interface中的任何一個(gè)來用。當(dāng)然啦,這個(gè)Dynamic Proxy其實(shí)就是一個(gè)Proxy,它不會(huì)替你作實(shí)質(zhì)性的工作,在生成它的實(shí)例時(shí)你必須提供一個(gè)handler,由它接管實(shí)際的工作。
代碼示例
創(chuàng)建接口:
/** * @CreateDate: 2019/6/17 14:52 * @Version: 1.0 */ public interface BuyService { String buyPhone(); String buyComputer(); }
創(chuàng)建實(shí)現(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)建啟動(dòng)類:
public class Bootstrap { public static void main(String[] args) { // 動(dòng)態(tài)代理實(shí)現(xiàn) ReflectionHandler reflectionHandler = new ReflectionHandler(new BuyServiceImpl()); BuyService proxy = reflectionHandler.getProxy(); String computer = proxy.buyComputer(); String phone = proxy.buyPhone(); System.out.println(computer + "\r\n" + phone); }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之HashMap源碼深入分析
Java HashMap是一種基于哈希表實(shí)現(xiàn)的鍵值對(duì)存儲(chǔ)結(jié)構(gòu),可以實(shí)現(xiàn)快速的數(shù)據(jù)查找和存儲(chǔ)。它是線程不安全的,但在單線程環(huán)境中運(yùn)行效率高,被廣泛應(yīng)用于Java開發(fā)中2023-04-04Java多線程之鎖的強(qiáng)化學(xué)習(xí)
Java多線程的鎖都是基于對(duì)象的,Java中的每一個(gè)對(duì)象都可以作為一個(gè)鎖。這篇文章主要來通過一下示例為大家強(qiáng)化一下鎖的相關(guān)知識(shí)的掌握,希望對(duì)大家有所幫助2023-02-02Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(38)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07Springboot整合多數(shù)據(jù)源代碼示例詳解
這篇文章主要介紹了Springboot整合多數(shù)據(jù)源代碼示例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java時(shí)間類Date類和Calendar類的使用詳解
這篇文章主要介紹了Java時(shí)間類Date類和Calendar類的使用詳解,需要的朋友可以參考下2017-08-08Springboot整合分頁插件PageHelper步驟解析
這篇文章主要介紹了Springboot整合分頁插件PageHelper步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Spring?Cache+Redis緩存數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了Spring?Cache+Redis緩存數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01