JAVA靜態&動態代理
阿新 • • 發佈:2017-08-17
line int rgs lose 動態創建 hashcode 構造函數 super ocl
具體場景
- 為了使代理類和被代理類對第三方有相同的函數,代理類和被代理類一般實現一個公共的interface,該interface定義如下
public interface Calculator { public Integer add(Integer num1, Integer num2); public Integer minus(Integer num1, Integer num2); }
- 被代理類定義如下
public class CalculatorImpl implements Calculator { @Override
- 代理需求:在add函數和minus函數調用前後分別輸出before invocation和after invocation字樣
靜態代理解決方案
- 代碼如下:簡單直接,無需贅言,如果calculator裏邊不僅有add和minus,還有divide,product,log,sin…呢,呵呵噠
public class StaticCalculatorProxy implements Calculator { Calculator obj;
動態代理解決方案
- 首先編寫實現InvocationHandler接口的類,用於請求轉發,實現如下
public class CalculatorHandler implements InvocationHandler { private Object obj; //被代理類 public CalculatorHandler(Object obj) { this.obj = obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("in calculatorhandler, before invocation"); Object ret = method.invoke(obj, args); //執行被代理類方法 System.out.println("in calculationhandler, after invocation"); return ret; } }
- 生成動態代理
CalculatorImpl calculatorImpl = new CalculatorImpl();//被代理類 CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorImpl); Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler); System.out.println(calculator.add(1,2)); System.out.println(calculator.minus(1, 2));
無論calculator中包含多少函數,動態代理只需實現一次,實際工程中,System.out.println(“in calculatorhandler, before invocation”)可能是加緩存,打日誌等操作
動態代理工作原理
- 為了搞清楚動態代理如何工作,首先看看生成的動態代理的代碼是什麽,借助[1]中ProxyUtil代碼
public class ProxyUtils { /** * Save proxy class to path * * @param path path to save proxy class * @param proxyClassName name of proxy class * @param interfaces interfaces of proxy class * @return */ public static boolean saveProxyClass(String path, String proxyClassName, Class[] interfaces) { if (proxyClassName == null || path == null) { return false; } // get byte of proxy class byte[] classFile = ProxyGenerator.generateProxyClass(proxyClassName, interfaces); FileOutputStream out = null; try { out = new FileOutputStream(path); out.write(classFile); out.flush(); return true; } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } return false; } }
- 得到了生成的動態代理代碼如下:
public final class $Proxy0 extends Proxy implements Calculator { public $Proxy0(InvocationHandler invocationhandler) { super(invocationhandler); } public final boolean equals(Object obj) { try { return ((Boolean)super.h.invoke(this, m1, new Object[] { obj })).booleanValue(); } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } public final String toString() { try { return (String)super.h.invoke(this, m2, null); } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } public final Integer minus(Integer integer, Integer integer1) { try { return (Integer)super.h.invoke(this, m4, new Object[] { integer, integer1 }); } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } public final Integer add(Integer integer, Integer integer1) { try { return (Integer)super.h.invoke(this, m3, new Object[] { integer, integer1 }); } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } public final int hashCode() { try { return ((Integer)super.h.invoke(this, m0, null)).intValue(); } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } private static Method m1; private static Method m2; private static Method m4; private static Method m3; private static Method m0; static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus", new Class[] { Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer") }); m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add", new Class[] { Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer") }); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); } catch(NoSuchMethodException nosuchmethodexception) { throw new NoSuchMethodError(nosuchmethodexception.getMessage()); } catch(ClassNotFoundException classnotfoundexception) { throw new NoClassDefFoundError(classnotfoundexception.getMessage()); } } }
- 有點長,按照初始化順序慢慢來分析,首先分析靜態代碼塊:
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus", new Class[] { Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer") }); m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add", new Class[] { Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer") }); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
得到公共interface中的add函數和minus函數對應的Method方法,同事也得到了equals,toString,hashCode三個函數的Method,所以調用代理類的equals,toString,hashCode也是要執行被代理類的方法的,知道這點很有必要
- 構造函數
public $Proxy0(InvocationHandler invocationhandler) { super(invocationhandler); }
初始化了內部的InvocationHandler變量,也就是下文的super.h
- 以add為例看一下請求的轉發
public final Integer add(Integer integer, Integer integer1) { try { return (Integer)super.h.invoke(this, m3, new Object[] { integer, integer1 }); } catch(Error _ex) { } catch(Throwable throwable) { throw new UndeclaredThrowableException(throwable); } }
super.h.invoke就是invocationhandler.invoke就是傳入的CalculatorHandler中實現的
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("in calculatorhandler, before invocation"); Object ret = method.invoke(obj, args); //執行被代理類方法 System.out.println("in calculationhandler, after invocation"); return ret; }
最終執行的就是CalculatorHandler對應的invoke函數
總結
Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler);
生成動態代理的過程步驟如下[2]:
// InvocationHandlerImpl 實現了 InvocationHandler 接口,並能實現方法調用從代理類到委托類的分派轉發 // 其內部通常包含指向委托類實例的引用,用於真正執行分派轉發過來的方法調用 InvocationHandler handler = new InvocationHandlerImpl(..); // 通過 Proxy 為包括 Interface 接口在內的一組接口動態創建代理類的類對象 Class clazz = Proxy.getProxyClass(classLoader, new Class[] { Interface.class, ... }); // 通過反射從生成的類對象獲得構造函數對象 Constructor constructor = clazz.getConstructor(new Class[] { InvocationHandler.class }); // 通過構造函數對象創建動態代理類實例 Interface Proxy = (Interface)constructor.newInstance(new Object[] { handler });
Proxy.newProxyInstance幫我們做了2,3,4步,直接返回給我們一個動態代理對象,代理對象最終執行InvocationHandler中invoke函數。順便強推文章[2]
References
- https://github.com/android-cn/android-open-project-demo/tree/master/java-dynamic-proxy
- https://www.ibm.com/developerworks/cn/java/j-lo-proxy1/index.html
JAVA靜態&動態代理