Java反射的應用之動態(tài)代理深入理解
更新時間:2021年09月13日 10:21:58 作者:威斯布魯克.猩猩
這篇文章主要介紹了Java反射的應用之動態(tài)代理深入理解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、代理模式的引入
靜態(tài)代理舉例
特點:代理類和被代理類在編譯期間,就確定下來了。
interface ClothFactory{ void produceCloth(); } //代理類 class ProxyClothFactory implements ClothFactory{ private ClothFactory factory;//用被代理類對象進行實例化 public ProxyClothFactory(ClothFactory factory){ this.factory = factory; } @Override public void produceCloth() { System.out.println("代理工廠做一些準備工作"); factory.produceCloth(); System.out.println("代理工廠做一些后續(xù)的收尾工作"); } } //被代理類 class NikeClothFactory implements ClothFactory{ @Override public void produceCloth() { System.out.println("Nike工廠生產一批運動服"); } } public class StaticProxyTest { public static void main(String[] args) { //創(chuàng)建被代理類的對象 NikeClothFactory nike = new NikeClothFactory(); //創(chuàng)建代理類的對象 ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nike); proxyClothFactory.produceCloth(); }
二、動態(tài)代理
動態(tài)代理的舉例
interface Human{ String getBelief(); void eat(String food); } //被代理類 class SuperMan implements Human{ @Override public String getBelief() { return "I believe I can fly!"; } @Override public void eat(String food) { System.out.println("我喜歡吃" + food); } } class HumanUtil{ public void method1(){ System.out.println("===================通用方法一==============================="); } public void method2(){ System.out.println("===================通用方法二==============================="); } } /* 要想實現(xiàn)動態(tài)代理,需要解決的問題? 問題一:如何根據(jù)加載到內存中的被代理類,動態(tài)的創(chuàng)建一個代理類及其對象。 問題二:當通過代理類的對象調用方法a時,如何動態(tài)的去調用被代理類中的同名方法a。 */ class ProxyFactory{ //調用此方法,返回一個代理類的對象。解決問題一 public static Object getProxyInstance(Object obj){//obj:被代理類的對象 MyInvocationHandler handler = new MyInvocationHandler(); handler.bind(obj); return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handler); } } class MyInvocationHandler implements InvocationHandler{ private Object obj;//需要使用被代理類的對象進行賦值 public void bind(Object obj){ this.obj = obj; } //當我們通過代理類的對象,調用方法a時,就會自動的調用如下的方法:invoke() //將被代理類要執(zhí)行的方法a的功能就聲明在invoke()中 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { HumanUtil util = new HumanUtil(); util.method1(); //method:即為代理類對象調用的方法,此方法也就作為了被代理類對象要調用的方法 //obj:被代理類的對象 Object returnValue = method.invoke(obj,args); util.method2(); //上述方法的返回值就作為當前類中的invoke()的返回值。 return returnValue; } } public class ProxyTest{ public static void main(String[] args) { SuperMan superMan = new SuperMan(); Human proxyInstance = (Human) ProxyFactory.getProxyInstance(superMan); //當通過代理類對象調用方法時,會自動的調用被代理類中同名的方法 String belief = proxyInstance.getBelief(); System.out.println(belief); proxyInstance.eat("四川麻辣燙"); System.out.println("************************************************"); NikeClothFactory nikeClothFactory = new NikeClothFactory(); ClothFactory proxyClothFactory = (ClothFactory) ProxyFactory.getProxyInstance(nikeClothFactory); proxyClothFactory.produceCloth(); }
到此這篇關于Java反射的應用之動態(tài)代理深入理解的文章就介紹到這了,更多相關Java動態(tài)代理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java常見問題之javac Hello.java找不到文件的解決方法
剛開始編寫java代碼時,肯定會遇到各種各樣的bug,當然對于初學者這也是能理解的,下面這篇文章主要給大家介紹了關于Java常見問題之javac Hello.java找不到文件解決的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下。2018-01-01Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01java學生管理系統(tǒng)界面簡單實現(xiàn)(全)
這篇文章主要為大家詳細介紹了java學生管理系統(tǒng)界面的簡單實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01SpringBoot項目中只執(zhí)行一次的任務寫法實現(xiàn)
有時候我們需要進行初始化工作,就說明只要進行一次的工作,本文主要介紹了SpringBoot項目中只執(zhí)行一次的任務寫法實現(xiàn),感興趣的可以了解一下2023-12-12