一文讀懂Java動態代理

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"封面 :洛小汐","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者 :潘潘","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事實上,對於很多Java編程人員來說,可能只需要達到從入門到上手的編程水準,就能很好的完成大部分研發工作。除非自己強主動獲取,或者工作倒逼你學習,否則我們好像沒必要去真正瞭解Java編程,或者深入研究JDK運行原理、或者在實際工作中某個模塊寫一套設計模式、或者糾結一個線程安全問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我覺得完全沒必要了解,因爲很多知識內容,我技術儲備上僅僅點到爲止,就能勝任工作,何必深入?確實,我也和有些朋友一樣,8年編程生涯以來大部分時候都存在這種思想,直到某一天突然有機會來了,你就要負責某個系統的架構規劃設計、你就要全權保障整個企業的信息化安全、你就要管理底下幾十上百號研發兄弟,但你知道你只擅長if/else,你清楚的知道你寫過一個下單方法有9000行代碼,你剛剛接到一個運維同事的電話,說你某個SQL執行後CPU飆到100%...","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/5512b32a293c3e2052849cad62250b55.jpeg","alt":"焦慮","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對,這就是你我現在或未來,都終將會面臨同時躲不過的問題,所以我想尋思着要不就來一個 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"「一文讀懂」","attrs":{}},{"type":"text","text":" 系列,我們深入淺出,跟大家一塊研究學習,從Java基礎到核心,從單體框架到微服務集羣,從數據庫到服務器,我們都一起分享,同步成長,Java之路,應不忘初心,終生學習。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"於是,有了我們第一話 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"「Java動態代理」","attrs":{}},{"type":"text","text":" 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3e/3ec9d5a77b3af936942ff1f4b07bb20a.png","alt":"在這裏插入圖片描述","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"引言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最早的代理模式,我們大致可以聯想到三國時期,孟德君挾天子以令諸侯是代理模式,是權利代理;現今生活中類似房產中介、票務中介是代理模式,是業務代理;還有翻牆瀏覽網頁是代理模式,是VPN代理;回到我們編程世界裏呢,以前你用的遠程方法調用(RMI)是代理、企業JavaBeans(EJB)是代理,現在流行的衆多RPC框架(如dubbo)也是代理,包括我們的Java動態代理,他們都是對象代理。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Java動態代理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java 動態代理機制的出現,使得 Java 開發人員不用手工編寫代理類,只要簡單地指定一組接口及委託類對象,便能動態地獲得代理類。代理類會負責將所有的方法調用分派到委託對象上","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"反射","attrs":{}},{"type":"text","text":"執行,在分派執行的過程中,開發人員還可以按需調整委託類對象及其功能,這是一套非常靈活有彈性的代理框架。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代理是一種常見的設計模式,其目的就是爲 ”調用方“ 提供一個代理類以控制對 ”被調用方“ 的訪問。代理類負責爲委託類預處理消息,過濾消息並轉發消息,以及進行消息被委託類執行後的後續處理。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3b/3bf19a4aee3174e495b10a2c6d902de7.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以發現,代理類與委託類,實現了同一個接口,所以對於客戶端請求來說沒有絲毫的區別,這也是Java面向接口編程的特點。代理模式使用代理對象(代理類)完成用戶請求,有效的屏蔽了用戶對真實對象(委託類)的訪問,也可以很好地隱藏和保護委託類對象。同時也爲添加不同控制策略爭取了空間,從而在設計上獲得了更大的靈活性。Java 動態代理機制以巧妙的方式近乎完美地實踐了代理模式的設計理念。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如現實世界的代理人被授權執行當事人的一些事宜,無需當事人出面,從第三方的角度看,似乎當事人並不存在,因爲他只和代理人通信。而事實上代理人是要有當事人的授權,並且在覈心問題上還需要請示當事人,同時代理人除了轉發完成當事人的授權事宜之外,還可以在執行授權事宜前後增加額外的一些事務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代理對象 = 目標對象 + 增強事務","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"相關類和接口","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/98/9826ff6a8738419ba750b227a5d45c57.png","alt":"java動態代理核心類/接口","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要了解 Java 動態代理的機制,首先需要了解兩個相關的類或接口:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"java.lang.reflect.Proxy:這是 Java 動態代理機制的主類,它提供了一組靜態方法來爲一組接口動態地生成代理類及其對象。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 1. Proxy 的靜態方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" \n // 方法 1: 該方法用於獲取指定代理對象所關聯的調用處理器\n static InvocationHandler \n getInvocationHandler(Object proxy)\n\n // 方法 2:該方法用於獲取關聯於指定類裝載器和一組接口的動態代理類的類對象\n static Class getProxyClass(\n ClassLoader loader, \n Class[] interfaces)\n\n // 方法 3:該方法用於判斷指定類對象是否是一個動態代理類\n static boolean isProxyClass(Class cl)\n\n // 方法 4:該方法用於爲指定類裝載器、一組接口及調用處理器生成動態代理類實例\n static Object newProxyInstance(\n ClassLoader loader, \n Class[] interfaces,\n InvocationHandler h)\n ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"java.lang.reflect.InvocationHandler:這是調用處理器接口,它自定義了一個 invoke 方法,用於集中處理在動態代理類對象上的方法調用,通常在該方法中實現對委託類的代理訪問。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 2. InvocationHandler 的核心方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" \n // 該方法負責集中處理動態代理類上的所有方法調用。\n // 第一個參數既是代理類實例,\n // 第二個參數是被調用的方法對象\n // 第三個方法是調用參數。\n // 調用處理器根據這三個參數\n // 進行預處理或分派到委託類實例上反射執行\n Object invoke(Object proxy, Method method, Object[] args)\n ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每次生成動態代理類對象時都需要指定一個實現了該接口的調用處理器對象(參見 Proxy 靜態方法 4 的第三個參數)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"java.lang.ClassLoader:這是類裝載器類,負責將類的字節碼裝載到 Java 虛擬機(JVM)中併爲其定義類對象,然後該類才能被使用。Proxy 靜態方法生成動態代理類同樣需要通過類裝載器來進行裝載才能使用,它與普通類的唯一區別就是其字節碼是由 JVM 在 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"運行時動態生成","attrs":{}},{"type":"text","text":" 的而非預存在於任何一個 .class 文件中。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每次生成動態代理類對象時都需要指定一個類裝載器對象(參見 Proxy 靜態方法 4 的第一個參數)","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"代理機制與特點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先讓我們來了解一下如何使用 Java 動態代理。具體有如下四步驟:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c3/c33fa205334812a941f767e310b552bb.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"通過實現 InvocationHandler 接口創建自己的調用處理器;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"通過爲 Proxy 類指定 ClassLoader 對象和一組 interface 來創建動態代理類;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"通過反射機制獲得動態代理類的構造函數,其唯一參數類型是調用處理器接口類型;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"通過構造函數創建動態代理類實例,構造時調用處理器對象作爲參數被傳入。","attrs":{}}]}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 3. InvocationHandler 的核心方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n// InvocationHandlerImpl 實現了 InvocationHandler 接口,\n// 並能實現方法調用從代理類到委託類的分派轉發\n// 其內部通常包含指向委託類實例的引用,\n// 用於真正執行分派轉發過來的方法調用\nInvocationHandler handler = new InvocationHandlerImpl(..);\n\n// 通過 Proxy 爲包括 Interface 接口在內的一組接口\n// 動態創建代理類的類對象\nClass clazz = Proxy.getProxyClass(\n classLoader, \n new Class[] { Interface.class, ... });\n\n// 通過反射從生成的類對象獲得構造函數對象\nConstructor constructor = clazz.getConstructor(\n new Class[] { InvocationHandler.class });\n\n// 通過構造函數對象創建動態代理類實例\nInterface Proxy = (Interface)constructor.newInstance(\n new Object[] { handler });\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實際使用過程更加簡單,因爲 Proxy 的靜態方法 newProxyInstance 已經爲我們封裝了步驟 2 到步驟 4 的過程,所以簡化後的過程如下","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 4. 簡化的動態代理對象創建過程(三合一)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n// InvocationHandlerImpl 實現了 InvocationHandler 接口,\n// 並能實現方法調用從代理類到委託類的分派轉發\nInvocationHandler handler = new InvocationHandlerImpl(..);\n\n// 通過 Proxy 直接創建動態代理類實例\nInterface proxy = (Interface)Proxy.newProxyInstance( \n classLoader,\n new Class[] { Interface.class },\n handler );\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來讓我們來了解一下 Java 動態代理機制的一些特點。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先是動態生成的代理類本身的一些特點。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"包:如果所代理的接口都是 public 的,那麼它將被定義在頂層包(即包路徑爲空),如果所代理的接口中有非 public 的接口(因爲接口不能被定義爲 protect 或 private,所以除 public 之外就是默認的 package 訪問級別),那麼它將被定義在該接口所在包(假設代理了 com.panshenlian.proxy 包中的某非 public 接口 A,那麼新生成的代理類所在的包就是 com.panshenlian.proxy ),這樣設計的目的是爲了最大程度的保證動態代理類不會因爲包管理的問題而無法被成功定義並訪問;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類修飾符:該代理類具有 final 和 public 修飾符,意味着它可以被所有的類訪問,但是不能被再度繼承;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類名:格式是”$ProxyN”,其中 N 是一個逐一遞增的阿拉伯數字,代表 Proxy 類第 N 次生成的動態代理類,值得注意的一點是,並不是每次調用 Proxy 的靜態方法創建動態代理類都會使得 N 值增加,原因是如果對同一組接口(包括接口排列的順序相同)試圖重複創建動態代理類,它會很聰明地返回先前已經創建好的代理類的類對象,而不會再嘗試去創建一個全新的代理類,這樣可以節省不必要的代碼重複生成,提高了代理類的創建效率。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類繼承關係:該類的繼承關係如圖:","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/55f05587cf4369cfd0b129a5107e1fb9.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 由圖可見,Proxy 類是它的父類,這個規則適用於所有由 Proxy 創建的動態代理類。而且該類還實現了其所代理的一組接口,這就是爲什麼它能夠被安全地類型轉換到其所代理的某接口的根本原因。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來讓我們瞭解一下代理類實例的一些特點。每個實例都會關聯一個調用處理器對象,可以通過 Proxy 提供的靜態方法 getInvocationHandler 去獲得代理類實例的調用處理器對象。在代理類實例上調用其代理的接口中所聲明的方法時,這些方法最終都會由調用處理器的 invoke 方法執行,此外,值得注意的是,代理類的根類 java.lang.Object 中有三個方法也同樣會被分派到調用處理器的 invoke 方法執行,它們是 hashCode,equals 和 toString,可能的原因有:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一是因爲這些方法爲 public 且非 final 類型,能夠被代理類覆蓋;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"二是因爲這些方法往往呈現出一個類的某種特徵屬性,具有一定的區分度,所以爲了保證代理類與委託類對外的一致性,這三個方法也應該被分派到委託類執行。當代理的一組接口有重複聲明的方法且該方法被調用時,代理類總是從排在最前面的接口中獲取方法對象並分派給調用處理器,而無論代理類實例是否正在以該接口(或繼承於該接口的某子接口)的形式被外部引用,因爲在代理類內部無法區分其當前的被引用類型。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着來了解一下被代理的一組接口有哪些特點。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,要注意不能有重複的接口,以避免動態代理類代碼生成時的編譯錯誤。其次,這些接口對於類裝載器必須可見,否則類裝載器將無法鏈接它們,將會導致類定義失敗。再次,需被代理的所有非 public 的接口必須在同一個包中,否則代理類生成也會失敗。最後,接口的數目不能超過 65535,這是 JVM 設定的限制。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" \n /**\n * Proxy.java\n * Generate a proxy class. Must call the checkProxyAccess method\n * to perform permission checks before calling this.\n */\n private static Class> getProxyClass0(\n ClassLoader loader,Class>... interfaces) {\n \n if (interfaces.length > 65535) {\n throw new IllegalArgumentException(\"interface limit exceeded\");\n }\n\n // If the proxy class defined by the given loader implementing\n // the given interfaces exists, this will simply return the cached copy;\n // otherwise, it will create the proxy class via the ProxyClassFactory\n return proxyClassCache.get(loader, interfaces);\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後再來了解一下異常處理方面的特點。從調用處理器接口聲明的方法中可以看到理論上它能夠拋出任何類型的異常,因爲所有的異常都繼承於 Throwable 接口,但事實是否如此呢?答案是否定的,原因是我們必須遵守一個繼承原則:即子類覆蓋父類或實現父接口的方法時,拋出的異常必須在原方法支持的異常列表之內。所以雖然調用處理器理論上講能夠,但實際上往往受限制,除非父接口中的方法支持拋 Throwable 異常。那麼如果在 invoke 方法中的確產生了接口方法聲明中不支持的異常,那將如何呢?放心,Java 動態代理類已經爲我們設計好了解決方法:它將會拋出 UndeclaredThrowableException 異常。這個異常是一個 RuntimeException 類型,所以不會引起編譯錯誤。通過該異常的 getCause 方法,還可以獲得原來那個不受支持的異常對象,以便於錯誤診斷。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"代碼演示","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"機制和特點都介紹過了,接下來讓我們通過源代碼來了解一下 Proxy 到底是如何實現的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先記住 Proxy 的幾個重要的靜態變量:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 5. Proxy 的重要靜態變量","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n// 映射表:用於維護類裝載器對象到其對應的代理類緩存\nprivate static Map loaderToCache = new WeakHashMap();\n\n// 標記:用於標記一個動態代理類正在被創建中\nprivate static Object pendingGenerationMarker = new Object();\n\n// 同步表:記錄已經被創建的動態代理類類型,主要被方法 isProxyClass 進行相關的判斷\nprivate static Map proxyClasses = Collections.synchronizedMap(new WeakHashMap());\n\n// 關聯的調用處理器引用\nprotected InvocationHandler h;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然後,來看一下 Proxy 的構造方法:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 6. Proxy 構造方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n// 由於 Proxy 內部從不直接調用構造函數,\n// 所以 private 類型意味着禁止任何調用\nprivate Proxy() {}\n\n// 由於 Proxy 內部從不直接調用構造函數,\n// 所以 protected 意味着只有子類可以調用\nprotected Proxy(InvocationHandler h) {this.h = h;}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着,可以快速瀏覽一下 newProxyInstance 方法,因爲其相當簡單:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 7. Proxy 靜態方法 newProxyInstance","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n/**\n * 將方法調用分派到指定調用處理器\n * 並返回指定接口的代理類實例\n */\npublic static Object newProxyInstance(\n ClassLoader loader,\n Class>[] interfaces,\n InvocationHandler h)\n throws IllegalArgumentException {\n\n // 檢查 h 不爲空,否則拋異常\n if (h == null) {\n throw new NullPointerException();\n }\n\n // 獲得與制定類裝載器和一組接口相關的代理類類型對象\n Class cl = getProxyClass(loader, interfaces);\n\n // 通過反射獲取構造函數對象並生成代理類實例\n try {\n \n Constructor cons = \n cl.getConstructor(constructorParams);\n \n return (Object) cons.newInstance(new Object[]{h});\n } catch (NoSuchMethodException e) { \n throw new InternalError(e.toString());\n } catch (IllegalAccessException e) { \n throw new InternalError(e.toString());\n } catch (InstantiationException e) { \n throw new InternalError(e.toString());\n } catch (InvocationTargetException e) { \n throw new InternalError(e.toString());\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由此可見,動態代理真正的關鍵是在 getProxyClass 方法,該方法負責爲一組接口動態地生成代理類類型對象。在該方法內部,您將能看到 Proxy 內的各路英雄(靜態變量)悉數登場。有點迫不及待了麼?那就讓我們一起走進 Proxy 最最神祕的殿堂去欣賞一番吧。該方法總共可以分爲四個步驟:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第 1 步,對這組接口進行一定程度的安全檢查,包括檢查接口類對象是否對類裝載器可見並且與類裝載器所能識別的接口類對象是完全相同的,還會檢查確保是 interface 類型而不是 class 類型。這個步驟通過一個循環來完成,檢查通過後將會得到一個包含所有接口名稱的字符串數組,記爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"String[] interfaceNames","attrs":{}}],"attrs":{}},{"type":"text","text":" 。總體上這部分實現比較直觀,所以略去大部分代碼,僅保留如何判斷某類或接口是否對特定類裝載器可見的相關代碼。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 8. 通過 Class.forName 方法判接口的可見性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n try {\n // 指定接口名字、類裝載器對象,\n // 同時制定 initializeBoolean \n // 爲 false 表示無須初始化類\n // \n // 如果方法返回正常這表示可見,\n // 否則會拋出 ClassNotFoundException \n // 異常表示不可見\n interfaceClass = \n Class.forName(interfaceName, false, loader);\n } catch (ClassNotFoundException e) {\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第 2 步,從 loaderToCache 映射表中獲取以類裝載器對象爲關鍵字所對應的緩存表,如果不存在就創建一個新的緩存表並更新到 loaderToCache。緩存表是一個 HashMap 實例,正常情況下它將存放鍵值對(接口名字列表,動態生成的代理類的類對象引用)。當代理類正在被創建時它會臨時保存(接口名字列表,pendingGenerationMarker)。標記 pendingGenerationMarke 的作用是通知後續的同類請求(接口數組相同且組內接口排列順序也相同)代理類正在被創建,請保持等待直至創建完成。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 9. 緩存表的使用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n do {\n // 以接口名字列表作爲關鍵字獲得對應 cache 值\n Object value = cache.get(key);\n if (value instanceof Reference) {\n proxyClass = \n (Class) ((Reference) value).get();\n }\n if (proxyClass != null) {\n // 如果已經創建,直接返回\n return proxyClass;\n } else if (value == pendingGenerationMarker) {\n // 代理類正在被創建,保持等待\n try {\n cache.wait();\n } catch (InterruptedException e) {\n }\n // 等待被喚醒後,\n // 再次循環檢查,\n // 以確保創建完成,\n // 否則重新等待\n continue;\n } else {\n // 標記代理類正在被創建\n cache.put(key, pendingGenerationMarker);\n // break 跳出循環已進入創建過程\n break;\n } while (true);\n \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第 3 步,動態創建代理類的類對象。首先是確定代理類所在的包,其原則如前所述,如果都爲 public 接口,則包名爲空字符串表示頂層包;如果所有非 public 接口都在同一個包,則包名與這些接口的包名相同;如果有多個非 public 接口且不同包,則拋異常終止代理類的生成。確定了包後,就開始生成代理類的類名,同樣如前所述按格式” ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"$ProxyN","attrs":{}},{"type":"text","text":" ”生成。類名也確定了,接下來就是見證奇蹟的發生 —— 動態生成代理類:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 10. 動態生成代理類","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n // 動態地生成代理類的字節碼數組\n byte[] proxyClassFile = \n ProxyGenerator.generateProxyClass( \n \t\tproxyName, \n \t\tinterfaces);\n try {\n \n // 動態地定義新生成的代理類\n proxyClass = \n defineClass0(\n \t\tloader, \n \t\tproxyName, \n \t\tproxyClassFile, 0,\n \t\tproxyClassFile.length);\n \n } catch (ClassFormatError e) {\n throw new IllegalArgumentException(e.toString());\n }\n\n // 把生成的代理類的類對象 \n // 記錄進 proxyClasses 表\n proxyClasses.put(proxyClass, null);\n\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由此可見,所有的代碼生成的工作都由神祕的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ProxyGenerator","attrs":{}},{"type":"text","text":" 所完成了,當你嘗試去探索這個類時,你所能獲得的信息僅僅是它位於並未公開的 sun.misc 包,有若干常量、變量和方法以完成這個神奇的代碼生成的過程,但是 sun 並沒有提供源代碼以供研讀。至於動態類的定義,則由 Proxy 的 native 靜態方法 defineClass0 執行。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第 4 步,代碼生成過程進入結尾部分,根據結果更新緩存表,如果成功則將代理類的類對象引用更新進緩存表,否則清除緩存表中對應關鍵值,最後喚醒所有可能的正在等待的線程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"走完了以上四個步驟後,至此,所有的代理類生成細節都已介紹完畢,剩下的靜態方法如 getInvocationHandler 和 isProxyClass 就顯得如此的直觀,只需通過查詢相關變量就可以完成,所以對其的代碼分析就省略了。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"代理類實現推演","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分析了 Proxy 類的源代碼,相信在讀者的腦海中會對 Java 動態代理機制形成一個更加清晰的理解,但是,當探索之旅在 sun.misc.ProxyGenerator 類處嘎然而止,所有的神祕都匯聚於此時,相信不少讀者也會對這個 ProxyGenerator 類產生有類似的疑惑:它到底做了什麼呢?它是如何生成動態代理類的代碼的呢?誠然,這裏也無法給出確切的答案。還是讓我們帶着這些疑惑,一起開始探索之旅吧。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9d/9d13e95e06f6c59524ec0e7f01e403fe.jpeg","alt":"代理類推演思考圖例","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事物往往不像其看起來的複雜,需要的是我們能夠化繁爲簡,這樣也許就能有更多撥雲見日的機會。拋開所有想象中的未知而複雜的神祕因素,如果讓我們用最簡單的方法去實現一個代理類,唯一的要求是同樣結合調用處理器實施方法的分派轉發,您的第一反應將是什麼呢?”聽起來似乎並不是很複雜”。的確,掐指算算所涉及的工作無非包括幾個反射調用,以及對原始類型數據的裝箱或拆箱過程,其他的似乎都已經水到渠成。非常地好,讓我們整理一下思緒,一起來完成一次完整的推演過程吧。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 11. 代理類中方法調用的分派轉發推演實現","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\n// 假設需代理接口 Simulator\npublic interface Simulator {\n short simulate(int arg1, long arg2, String arg3) \n throws ExceptionA, ExceptionB;\n}\n\n// 假設代理類爲 SimulatorProxy, 其類聲明將如下\nfinal public class SimulatorProxy implements Simulator {\n\n // 調用處理器對象的引用\n protected InvocationHandler handler;\n\n // 以調用處理器爲參數的構造函數\n public SimulatorProxy(InvocationHandler handler){\n this.handler = handler;\n }\n\n // 實現接口方法 simulate\n public short simulate(int arg1, long arg2, String arg3)\n throws ExceptionA, ExceptionB {\n\n // 第一步是獲取 simulate 方法的 Method 對象\n java.lang.reflect.Method method = null;\n try{\n method = Simulator.class.getMethod(\n \"simulate\",\n new Class[] {int.class, long.class, String.class} );\n } catch(Exception e) {\n // 異常處理 1(略)\n }\n\n // 第二步是調用 handler 的 invoke 方法分派轉發方法調用\n Object r = null;\n try {\n r = handler.invoke(this,\n method,\n // 對於原始類型參數需要進行裝箱操作\n new Object[] {new Integer(arg1), new Long(arg2), arg3});\n }catch(Throwable e) {\n // 異常處理 2(略)\n }\n // 第三步是返回結果(返回類型是原始類型則需要進行拆箱操作)\n return ((Short)r).shortValue();\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"模擬推演爲了突出通用邏輯所以更多地關注正常流程,而淡化了錯誤處理,但在實際中錯誤處理同樣非常重要。從以上的推演中我們可以得出一個非常通用的結構化流程:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第一步從代理接口獲取被調用的方法對象;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二步分派方法到調用處理器執行;","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第三步返回結果。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這之中,所有的信息都是可以已知的,比如接口名、方法名、參數類型、返回類型以及所需的裝箱和拆箱操作,那麼既然我們手工編寫是如此,那又有什麼理由不相信 ProxyGenerator 不會做類似的實現呢?至少這是一種比較可能的實現。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來讓我們把注意力重新回到先前被淡化的錯誤處理上來。在異常處理 1 處,由於我們有理由確保所有的信息如接口名、方法名和參數類型都準確無誤,所以這部分異常發生的概率基本爲零,所以基本可以忽略。而異常處理 2 處,我們需要思考得更多一些。回想一下,接口方法可能聲明支持一個異常列表,而調用處理器 invoke 方法又可能拋出與接口方法不支持的異常,再回想一下先前提及的 Java 動態代理的關於異常處理的特點,對於不支持的異常,必須拋 UndeclaredThrowableException 運行時異常。所以通過再次推演,我們可以得出一個更加清晰的異常處理 2 的情況:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"清單 12. 細化的異常處理 2","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"\nObject r = null;\n\ntry {\n r = handler.invoke(\n this,\n method,\n new Object[] {\n new Integer(arg1), \n new Long(arg2), \n arg3\n }\n );\n\n} catch( ExceptionA e) {\n\n // 接口方法支持 ExceptionA,可以拋出\n throw e;\n\n} catch( ExceptionB e ) {\n // 接口方法支持 ExceptionB,可以拋出\n throw e;\n\n} catch(Throwable e) {\n // 其他不支持的異常,\n // 一律拋 UndeclaredThrowableException\n throw new UndeclaredThrowableException(e);\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣我們就完成了對動態代理類的推演實現。推演實現遵循了一個相對固定的模式,可以適用於任意定義的任何接口,而且代碼生成所需的信息都是可知的,那麼有理由相信即使是機器自動編寫的代碼也有可能延續這樣的風格,至少可以保證這是可行的。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"美中不足","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"誠然,Proxy 已經設計得非常優美,但是還是有一點點小小的遺憾之處,那就是它始終無法擺脫僅支持 interface 代理的桎梏,因爲它的設計註定了這個遺憾。回想一下那些動態生成的代理類的繼承關係圖,它們已經註定有一個共同的父類叫 Proxy。Java 的繼承機制註定了這些動態代理類們無法實現對 class 的動態代理,原因是多繼承在 Java 中本質上就行不通。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有很多條理由,人們可以否定對 class 代理的必要性,但是同樣有一些理由,相信支持 class 動態代理會更美好。接口和類的劃分,本就不是很明顯,只是到了 Java 中才變得如此的細化。如果只從方法的聲明及是否被定義來考量,有一種兩者的混合體,它的名字叫抽象類。實現對抽象類的動態代理,相信也有其內在的價值。此外,還有一些歷史遺留的類,它們將因爲沒有實現任何接口而從此與動態代理永世無緣。如此種種,不得不說是一個小小的遺憾。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,不完美並不等於不偉大,偉大是一種本質,Java 動態代理就是佐例。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"-- 來自IBM Developer 王忠平, 何平","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代理模式是先人們針對一類特定問題總結出的經驗結晶,並在各個領域中得以靈活應用。特別是在編程領域,不同語言根據自身的設計規範和特點融會貫通,最終都能夠落實到具體的解決方案中,譬如Spring AOP在性能事務上的增強提升,或者是攔截器實現在日誌和權限層面的控制過濾等等,都能做到對原接口事務的無侵入,同時還能靈活管控,大範圍實施,達到我們的預期,這就是代理模式,巧妙之處。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"感謝參考文獻與工具支持","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[1]Java 動態代理機制分析及擴展:https://developer.ibm.com/zh/articles/j-lo-proxy1/","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[2]代理模式原理及實例講解:https://developer.ibm.com/zh/articles/j-lo-proxy-pattern/","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[3]Dynamic Proxy Classes:https://java.sun.com/j2se/1.4.2/docs/guide/reflection/proxy.html","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[4]動態代理機制:https://www.ibm.com/developerworks/cn/java/j-jtp08305.html","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[5]圖片素材來源:https://www.pexels.com/","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[6]流程圖設計來源:https://www.processon.com/","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/77/77492ef944a59214afde31b710017bca.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"BIU ~","attrs":{}},{"type":"text","text":" 文章持續更新,微信搜索「","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"潘潘和他的朋友們","attrs":{}},{"type":"text","text":"」第一時間閱讀,隨時有驚喜。本文會在 GitHub ","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/senlypan/JavaWorld","title":""},"content":[{"type":"text","text":"https://github.com/JavaWorld","attrs":{}}]},{"type":"text","text":" 收錄,熱騰騰的技術、框架、面經、解決方案,我們都會以最美的姿勢第一時間送達,歡迎 Star。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章