Java靜態(tài)代理與動態(tài)代理案例詳解
代理模式
代理模式(Proxy):為其他對象提供一個代理以控制對這個對象的訪問。
主要解決:在直接訪問對象時帶來的問題,比如說:要訪問的對象在遠程的機器上。在面向?qū)ο笙到y(tǒng)中,有些對象由于某些原因(比如對象創(chuàng)建開銷很大,或者某些操作需要安全控制,或者需要進程外的訪問),直接訪問會給使用者或者系統(tǒng)結構帶來很多麻煩,我們可以在訪問此對象時加上一個對此對象的訪問層。
代理模式的元素是:共同接口、代理對象、目標對象。
代理模式的行為:由代理對象執(zhí)行目標對象的方法、由代理對象擴展目標對象的方法。
代理模式的宏觀特性:對客戶端只暴露出接口,不暴露它以下的架構。
好處多多:中間隔離了一層,更加符合開閉原則
UML圖
創(chuàng)建一個接口
/** * @Author: Promsing * @Date: 2021/4/3 - 8:25 * @Description: 買車的接口 * @version: 1.0 */ public interface BuyCar { public void buyCar(); }
創(chuàng)建一個實現(xiàn)類
/** * @Author: Promsing * @Date: 2021/4/3 - 8:25 * @Description: 實現(xiàn)類 * @version: 1.0 */ public class BuyCarImpl implements BuyCar { @Override public void buyCar() { System.out.println("我要買車~~~啦啦啦"); } }
靜態(tài)代理:
創(chuàng)建一個代理類
/** * @Author: Promsing * @Date: 2021/4/3 - 8:26 * @Description: 代理類 * @version: 1.0 */ public class BuyCarProxy implements BuyCar{ private BuyCar buyCar; //注意事final修飾的關鍵字 不可修改 //構造函數(shù)注入,需要被代理的對象 public BuyCarProxy(final BuyCar buyCar) { this.buyCar = buyCar; } //靜態(tài)代理- 的實現(xiàn)方式 @Override public void buyCar() { System.out.println("不貸款,全款!買車前的準備~~~"); buyCar.buyCar(); System.out.println("買完車了,出去浪~~~"); } }
客戶端調(diào)用
/** * @Author: Promsing * @Date: 2021/4/3 - 8:36 * @Description: 客戶端調(diào)用 * @version: 1.0 */ public abstract class ProxyTest implements BuyCar { public static void main(String[] args) { System.out.println("-+-+-+正常調(diào)用-+-+-+"); BuyCar car=new BuyCarImpl(); car.buyCar(); System.out.println("-+-+-+使用靜態(tài)代理-+-+-+"); BuyCar proxy=new BuyCarProxy(car); proxy.buyCar(); } } -+-+-+正常調(diào)用-+-+-+ 我要買車~~~啦啦啦 -+-+-+使用靜態(tài)代理-+-+-+ 不貸款,全款!買車前的準備~~~ 我要買車~~~啦啦啦 買完車了,出去浪~~~
動態(tài)代理:
基于接口的動態(tài)代理類
特點:字節(jié)碼隨用隨創(chuàng)建,隨用隨加載
作用:在不修改源碼的基礎上對方法增強
涉及的類:JDK官方提供的Proxy
如何創(chuàng)建代理對象:使用Proxy類中的newProxyInstance方法
創(chuàng)建代理對象的要求:被代理類至少實現(xiàn)一個接口
newProxyInstance方法的參數(shù)
ClassLoader:類加載器,同于加載被代理對象字節(jié)碼
Class[]:字節(jié)碼數(shù)組---用于讓代理對象和被代理對象擁有相同的方法
InvocationHandler:用于提供被增強的代碼
/** * @Author: Promsing * @Date: 2021/4/3 - 9:09 * @Description: 描述 形容 * @version: 1.0 */ public class DynamicProxy implements InvocationHandler { private BuyCar object; public DynamicProxy( BuyCar object) { this.object = object; } /** * * @param proxy 代理對象的引用 * @param method 當前執(zhí)行的方法 * @param args 當前執(zhí)行方法所需的參數(shù) * @return 和被代理對象方法有相同的返回值 * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("不貸款,全款!買車前的準備~~~"); Object result = method.invoke(object, args); System.out.println("買完車了,出去浪~~~"); return result; } }
客戶端
public static void main(String[] args) { System.out.println("-+-+-+使用基于接口的代理-+-+-+"); //方式一、如不寫動態(tài)代理類DynamicProxy,可以在這里使用內(nèi)部類 //聲明一個final修飾的對象 /* final BuyCarImpl car=new BuyCarImpl(); BuyCar proxy=(BuyCar)Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("不貸款,全款!買車前的準備~~~"); Object result = method.invoke(car, args); System.out.println("買完車了,出去浪~~~"); return result; } }); proxy.buyCar(); */ //方式二、使用DynamicProxy類 //聲明一個final修飾的對象 final BuyCarImpl car=new BuyCarImpl(); BuyCar proxy=(BuyCar)Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(),new DynamicProxy(car)); proxy.buyCar(); }
基于子類的動態(tài)代理
特點:字節(jié)碼隨用隨創(chuàng)建,隨用隨加載
作用:在不修改源碼的基礎上對方法增強
涉及的類:第三方cglib提供的Enhancer
如何創(chuàng)建代理對象:使用Enhancer類中create方法
創(chuàng)建代理對象的要求:被代理類不能是最終類
newProxyInstance方法的參數(shù)
Class:用于被指定代理對象的字節(jié)碼
InvocationHandler:用于提供增強的方法
public static void main(String[] args) { //使用基于子類的動態(tài)代理 //需要引入Jar包--cglib 本案例使用cglib3.3.0 System.out.println("-+-+-+使用基于子類的代理-+-+-+"); final BuyCarImpl car=new BuyCarImpl(); BuyCar proxy= (BuyCar)Enhancer.create(car.getClass(), new MethodInterceptor() { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("不貸款,全款!買車前的準備~~~"); Object result = method.invoke(car, args); System.out.println("買完車了,出去浪~~~"); return result; } }); proxy.buyCar(); }
到此這篇關于Java靜態(tài)代理與動態(tài)代理案例詳解的文章就介紹到這了,更多相關Java靜態(tài)代理與動態(tài)代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合sharding-jdbc實現(xiàn)自定義分庫分表的實踐
本文主要介紹了SpringBoot整合sharding-jdbc實現(xiàn)自定義分庫分表的實踐,將通過自定義算法來實現(xiàn)定制化的分庫分表來擴展相應業(yè)務,感興趣的可以了解一下2021-11-11Spring Boot中自定義注解結合AOP實現(xiàn)主備庫切換問題
這篇文章主要介紹了Spring Boot中自定義注解+AOP實現(xiàn)主備庫切換的相關知識,本篇文章的場景是做調(diào)度中心和監(jiān)控中心時的需求,后端使用TDDL實現(xiàn)分表分庫,需要的朋友可以參考下2019-08-08Windows7下的Java運行環(huán)境搭建過程圖解
這篇文章主要介紹了Windows7下的Java運行環(huán)境搭建過程圖解,需要的朋友可以參考下2014-04-04詳解java爬蟲jsoup解析多空格class數(shù)據(jù)
在本篇內(nèi)容中小編給大家分享了java爬蟲jsoup怎么解析多空格class數(shù)據(jù)的方法和技巧,需要的朋友們跟著學習下。2018-12-12Mybatis如何按順序查詢出對應的數(shù)據(jù)字段
這篇文章主要介紹了Mybatis如何按順序查詢出對應的數(shù)據(jù)字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01SpringCloud筆記(Hoxton)Netflix之Ribbon負載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負載均衡,Ribbon是管理HTTP和TCP服務客戶端的負載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對SpringCloud?Ribbon負載均衡相關知識感興趣的朋友一起看看吧2022-06-06