Java動態代理的應用詳解
動態代理其實就是java.lang.reflect.Proxy類別動態的根據您指定的所有介面產生一個class byte,該class會繼承Proxy類,並實作所有你指定的介面(您在參數中傳入的介面陣列);然後再利用您指定的classloader將 class byte載入進系統,最後產生這樣一個類別的對象,並初始化該對象的一些值,如invocationHandler,以即所有的介面對應的Method成員。 初始化之後將物件傳回給呼叫的客戶端。這樣客戶端拿到的就是一個實作你所有的介面的Proxy物件。請看實例分析:
package com.fans.common.proxy; public interface BusinessProcessor { public void processBusiness(); }
package com.fans.common.proxy; /** * 业务处理类 * @author fanshadoop * */ public class BusinessProcessorImpl implements BusinessProcessor { @Override public void processBusiness() { System.out.println("processing business....."); } }
package com.fans.common.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; /** * 业务代理类 * @author fanshadoop * */ 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; } }
package com.fans.common.proxy; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; public class Test { /** * @param args */ 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()); printClassDefinition(bp.getClass()); } 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("}"); } }
運行結果:
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.fans.common.proxy.BusinessProcessor { java.lang.reflect.Method m1; java.lang.reflect.Method m3; java.lang.reflect.Method m0; java.lang.reflect.Method m2; boolean equals(java.lang.Object); java.lang.String toString(); int hashCode(); void processBusiness(); }
類BusinessProcessorHandler實作了InvocationHandler介面的invoke方法,這個類別就是Proxy最終呼叫固定介面方法。
很明顯,Proxy.newProxyInstance方法會做以下幾件事:
1,根據傳入的第二個參數interfaces動態產生一個類,實作interfaces中的接口,該例中即BusinessProcessor接口的processBusiness方法。並且繼承了Proxy類,重寫了hashcode,toString,equals等三個方法。具體實現可參看
ProxyGenerator.generateProxyClass(...); 此範例中產生了$Proxy0類別
2,並透過傳入的第一個參數classloder將剛產生的類別載入jvm。即將$Proxy0類別load
3,利用第三個參數,呼叫$Proxy0的$Proxy0(InvocationHandler)建構子建立$Proxy0的對象,並且用interfaces參數遍歷其所有介面的方法,並產生Method對像初始化物件的幾個Method成員變數
4,將$Proxy0的執行個體傳回給客戶端。
現在好了。我們再看客戶端怎麼調就清楚了。
1,客戶端拿到的是$Proxy0的實例對象,由於$Proxy0繼承了BusinessProcessor,因此轉換為BusinessProcessor沒任何問題。
BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(....);
2,bp.processBusiness(); ()的實現就是透過InvocationHandler去呼叫invoke方法啦!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4
