java高级----Java动态代理的原理
Java動態代理機制的出現,使得 Java 開發人員不用手工編寫代理類,只要簡單地指定一組接口及委托類對象,便能動態地獲得代理類。代理類會負責將所有的方法調用分派到委托對象上反射執行,在分派執行的過程中,開發人員還可以按需調整委托類對象及其功能,這是一套非常靈活有彈性的代理框架。下面我們開始動態代理的學習。
?
目錄導航
?
動態代理的簡要說明
在java的動態代理機制中,有兩個重要的類或接口,一個是 InvocationHandler(Interface)、另一個則是 Proxy(Class)。
一、 InvocationHandler(interface)的描述:
InvocationHandler is the interface implemented by the invocation handler of a proxy instance. Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.?每一個動態代理類都必須要實現InvocationHandler這個接口,并且每個代理類的實例都關聯到了一個handler,當我們通過代理對象調用 一個方法的時候,這個方法的調用就會被轉發為由InvocationHandler這個接口的 invoke 方法來進行調用。我們來看看InvocationHandler這個接口的唯一一個方法?invoke 方法:
Object invoke(Object proxy, Method method, Object[] args) throws Throwable這個方法接收三個參數和返回一個Object類型,它們分別代表的意思如下:
- proxy: 指代我們所代理的那個真實對象
- method: 指代的是我們所要調用真實對象的方法的Method對象
- args: 指代的是調用真實對象某個方法時接受的參數
返回的Object是指真實對象方法的返回類型,以上會在接下來的例子中加以深入理解。
the value to return from the method invocation on the proxy instance.?
二、 Proxy(Class)的描述:
Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.Proxy這個類的作用就是用來動態創建一個代理對象。我們經常使用的是newProxyInstance這個方法:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException?參數的理解:
// 一個ClassLoader對象,定義了由哪個ClassLoader對象來對生成的代理對象進行加載 loader - the class loader to define the proxy class // 一個Interface對象的數組,表示的是我將要給我需要代理的對象提供一組什么接口 interfaces - the list of interfaces for the proxy class to implement // 一個InvocationHandler對象,表示的是當我這個動態代理對象在調用方法的時候,會關聯到哪一個InvocationHandler對象上 h - the invocation handler to dispatch method invocations to?返回結果的理解: 一個代理對象的實例
a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces?
簡單的Java代理
我們創建一個Java項目用于對動態代理的測試與理解,項目結構如下:
一、 先定義一個接口Interface,添加兩個方法。
package com.huhx.proxy;public interface Interface {void getMyName();String getNameById(String id); }?
二、 定義一個真實的實現上述接口的類,RealObject:
package com.huhx.proxy;public class RealObject implements Interface {@Overridepublic void getMyName() {System.out.println("my name is huhx");}@Overridepublic String getNameById(String id) {System.out.println("argument id: " + id);return "huhx";} }?
三、 定義一個代理對象,也實現了上述的Interface接口:
package com.huhx.proxy;public class SimpleProxy implements Interface {private Interface proxied;public SimpleProxy(Interface proxied) {this.proxied = proxied;}@Overridepublic void getMyName() {System.out.println("proxy getmyname");proxied.getMyName();}@Overridepublic String getNameById(String id) {System.out.println("proxy getnamebyid");return proxied.getNameById(id);} }?
四、 SimpleMain在Main方法中,測試上述的結果:
package com.huhx.proxy;public class SimpleMain {private static void consume(Interface iface) {iface.getMyName();String name = iface.getNameById("1");System.out.println("name: " + name);}public static void main(String[] args) {consume(new RealObject());System.out.println("========================================================");consume(new SimpleProxy(new RealObject()));} }?
五、 運行的結果如下:
my name is huhx argument id: 1 name: huhx ======================================================== proxy getmyname my name is huhx proxy getnamebyid argument id: 1 name: huhx?
Java的動態代理
完成了上述簡單的Java代理,現在我們開始學習Java的動態代理,它比代理的思想更向前一步,因為它可以動態地創建代理并動態的處理對所代理方法的調用。在動態代理上所做的所有調用都會被重定向到單一的調用處理器上,它的工作是揭示調用的類型并確定相應的對策。下面我們通過案例來加深Java動態代理的理解:
一、 創建一個繼承了InvocationHandler的處理器:DynamicProxyHandler
package com.huhx.dynamicproxy;import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.Arrays;public class DynamicProxyHandler implements InvocationHandler {private Object proxied;public DynamicProxyHandler(Object proxied) {System.out.println("dynamic proxy handler constuctor: " + proxied.getClass());this.proxied = proxied;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("dynamic proxy name: " + proxy.getClass());System.out.println("method: " + method.getName());System.out.println("args: " + Arrays.toString(args));Object invokeObject = method.invoke(proxied, args);if (invokeObject != null) {System.out.println("invoke object: " + invokeObject.getClass());} else {System.out.println("invoke object is null");}return invokeObject;} }?
?二、 我們寫一個測試的Main方法,DynamicProxyMain:
package com.huhx.dynamicproxy;import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy;import com.huhx.proxy.Interface; import com.huhx.proxy.RealObject;public class DynamicProxyMain {public static void consumer(Interface iface) {iface.getMyName();String name = iface.getNameById("1");System.out.println("name: " + name);}public static void main(String[] args) throws Exception, SecurityException, Throwable {RealObject realObject = new RealObject();consumer(realObject);System.out.println("==============================");// 動態代理ClassLoader classLoader = Interface.class.getClassLoader();Class<?>[] interfaces = new Class[] { Interface.class };InvocationHandler handler = new DynamicProxyHandler(realObject);Interface proxy = (Interface) Proxy.newProxyInstance(classLoader, interfaces, handler);System.out.println("in dynamicproxyMain proxy: " + proxy.getClass());consumer(proxy);} }?
三、 運行結果如下:
my name is huhx argument id: 1 name: huhx ============================== dynamic proxy handler constuctor: class com.huhx.proxy.RealObject in dynamicproxyMain proxy: class com.sun.proxy.$Proxy0 dynamic proxy name: class com.sun.proxy.$Proxy0 method: getMyName args: null my name is huhx invoke object is null dynamic proxy name: class com.sun.proxy.$Proxy0 method: getNameById args: [1] argument id: 1 invoke object: class java.lang.String name: huhx從以上輸出結果,我們可以得出以下結論:
- 與代理對象相關聯的InvocationHandler,只有在代理對象調用方法時,才會執行它的invoke方法
- invoke的三個參數的理解:Object proxy是代理的對象, Method method是真實對象中調用方法的Method類, Object[] args是真實對象中調用方法的參數
?
Java動態代理的原理
一、 動態代理的關鍵代碼就是Proxy.newProxyInstance(classLoader, interfaces, handler),我們跟進源代碼看看:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException {// handler不能為空if (h == null) {throw new NullPointerException();}final Class<?>[] intfs = interfaces.clone();final SecurityManager sm = System.getSecurityManager();if (sm != null) {checkProxyAccess(Reflection.getCallerClass(), loader, intfs);}/** Look up or generate the designated proxy class.*/
// 通過loader和接口,得到代理的Class對象Class<?> cl = getProxyClass0(loader, intfs);/** Invoke its constructor with the designated invocation handler.*/try {final Constructor<?> cons = cl.getConstructor(constructorParams);final InvocationHandler ih = h;if (sm != null && ProxyAccessHelper.needsNewInstanceCheck(cl)) {// create proxy instance with doPrivilege as the proxy class may// implement non-public interfaces that requires a special permissionreturn AccessController.doPrivileged(new PrivilegedAction<Object>() {public Object run() {return newInstance(cons, ih);}});} else {
// 創建代理對象的實例return newInstance(cons, ih);}} catch (NoSuchMethodException e) {throw new InternalError(e.toString());} }
?
二、 我們看一下newInstance方法的源代碼:
private static Object newInstance(Constructor<?> cons, InvocationHandler h) {try {return cons.newInstance(new Object[] {h} );} catch (IllegalAccessException | InstantiationException e) {throw new InternalError(e.toString());} catch (InvocationTargetException e) {Throwable t = e.getCause();if (t instanceof RuntimeException) {throw (RuntimeException) t;} else {throw new InternalError(t.toString());}} }?
三、 當我們通過代理對象調用 一個方法的時候,這個方法的調用就會被轉發為由InvocationHandler這個接口的 invoke 方法來進行調用。體現這句話的代碼,我在源碼中沒有找到,于是我在測試類的main方法中加入以下代碼:
if (proxy instanceof Proxy) {InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);invocationHandler.invoke(proxy, realObject.getClass().getMethod("getMyName"), null); System.out.println("--------------------------------------"); }這段代碼的輸出結果如下,與上述中調用代理對象中的getMyName方法輸出是一樣的,不知道Jvm底層是否是這樣判斷的:
dynamic proxy handler constuctor: class com.huhx.proxy.RealObject dynamic proxy name: class com.sun.proxy.$Proxy0 method: getMyName args: null my name is huhx invoke object is null --------------------------------------?
友情鏈接
- 測試動態代理的源代碼下載??? 訪問密碼 7d08
?
作者:?huhx?出處:?www.cnblogs.com/huhx
總結
以上是生活随笔為你收集整理的java高级----Java动态代理的原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java基础----Java的格式化输出
- 下一篇: Java学习之容器上(Collectio