解說:給某一個對象提供一個代理,並由代理對象控制對原對象的引用;
代理模式需要以下幾個角色:
1 主題:規定代理類和真實對象共同對外暴露的接口;
2 代理類:專門代理真實對象的類;
3 真實對象:需要被代理的對象;
代理解決的主要的業務就是需要在 真實對象的某個接口 前後處理一些事情,框架中多會用到這種功能,比如 打日志、記錄時間等
靜態代理是指自己動手編寫代碼實現代理類;
優點:業務類只需要關注業務邏輯本身,保證了業務類的重用性。這是代理的共有優點。
缺點:每一個真實對象都需要一個具體的代理類,不能做到可重用;
靜態代理比較簡單,下邊用代碼來具體說明;
主題接口:IAnimal
public interface IAnimal { /** * 動物叫 */ void bark(); }
真實對象:Dog
public class Dog implements IAnimal { private String name; public Dog(String name) { this.name = name; } @Override public void bark() { System.out.println(this.name + " bark:wang wang wang ... "); } }
代理:DogProxy
public class DogProxy implements IAnimal { private Dog dog; public DogProxy(Dog dog) { this.dog = dog; } @Override public void bark() { long l = System.currentTimeMillis(); System.out.println("dog will bark..."); this.dog.bark(); System.out.println("dog has barked which takes " + (System.currentTimeMillis() - l) + " ms !"); } }
靜態代理使用:
public class StaticProxyTest { public static void main(String[] args) { IAnimal dog = new Dog("大黃"); IAnimal dogProxy = new DogProxy(dog); dogProxy.bark(); } }
代理和真實對象對外暴露一致
動態代理是指在運行時動態生成代理類;
要使用Java中原生的動態代理,需要用到以下幾個類和接口
我們還是用靜態代理用到的代碼:主題接口IAnimal和真實對象Dog不變,去掉DogProxy和StaticProxyTest,增加以下代碼
DogProxyInvocationHandler
public class DogProxyInvocationHandler implements InvocationHandler { private Object animal; public DogProxyInvocationHandler(Object animal) { this.animal = animal; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("proxy class:" + proxy.getClass() + ",class:" + getClass() + ",method:" + method); Object obj = method.invoke(animal, args); System.out.println("obj:" + obj); return obj; } }
DynamicProxyTest
public class DynamicProxyTest { public static void main(String[] args) { IAnimal dog = new Dog("大黃"); InvocationHandler invocationHandler = new DogProxyInvocationHandler(dog); IAnimal animal = (IAnimal) Proxy.newProxyInstance(dog.getClass().getClassLoader(), dog.getClass().getInterfaces(), invocationHandler); animal.bark(); } }
可以看出:Java動態代理 我們必須有真實對象,實現了InvocationHandler接口的自己的處理類,然後通過Proxy生成代理類
輸出如下:
proxy class:class com.sun.proxy.$Proxy0,class:class com.shock.base.proxy.dynamic.DogProxyInvocationHandler,method:public abstract void com.shock.base.proxy.dynamic.IAnimal.bark()
大黃 bark:wang wang wang ...這裡動態代理的優勢相比靜態代理為:即使真實對象有N個接口,我們的invocationHandler只需要一個Invoke方法即可!
這裡有幾個問題:
1 動態代理生成的class name為什麼是 $Proxy0 ?
如圖,相關變量如下:
以上便可以解決相關問題
2 動態代理生成的代理類到底是什麼樣子的?生成代理類的關鍵接口是什麼?為何調用真實對象的某個接口會進入invoke方法?
通過分析源碼:我們知道 Proxy.newProxyInstance → Proxy.getProxyClass0 → WeakCache.get → WeakCache.Factory.get → Proxy.ProxyClassFactory.apply → ProxyGenerator.generateProxyClass
最終生成了一個 byte[] 類型的 class類;這樣byte[] 比較抽象 ,我們想看到該怎麼辦?可以通過下邊的代碼生成Proxy0
int accessFlags = Modifier.PUBLIC | Modifier.FINAL; byte[] bytes = ProxyGenerator.generateProxyClass("com.sun.proxy.$Proxy0", new Class[]{IAnimal.class}, accessFlags); FileOutputStream fileOutputStream = new FileOutputStream(new File( "~/work/$Proxy0.class")); fileOutputStream.write(bytes); fileOutputStream.flush(); fileOutputStream.close();
生成的代碼如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.sun.proxy; import com.shock.base.proxy.dynamic.IAnimal; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy implements IAnimal { private static Method m1; private static Method m2; private static Method m3; private static Method m0; public $Proxy0(InvocationHandler var1) throws { super(var1); } public final boolean equals(Object var1) throws { try { return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue(); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final String toString() throws { try { return (String)super.h.invoke(this, m2, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final void bark() throws { try { super.h.invoke(this, m3, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final int hashCode() throws { try { return ((Integer)super.h.invoke(this, m0, (Object[])null)).intValue(); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } 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]); m3 = Class.forName("com.shock.base.proxy.dynamic.IAnimal").getMethod("bark", new Class[0]); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); } catch (NoSuchMethodException var2) { throw new NoSuchMethodError(var2.getMessage()); } catch (ClassNotFoundException var3) { throw new NoClassDefFoundError(var3.getMessage()); } } } View Code以上代碼為我們解答了紅色的問題。雖然代碼是這樣的 代理類集成了 Proxy類,但是如果想要驗證 如何驗證呢?
cglib是什麼?CGLIB is a powerful, high performance code generation library.
特點簡單說:
引入JAR包支持,如下:
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.4</version> </dependency>
目前最新的版本是 3.2.4,CGLib的package分布和作用如下:
我們沿用上邊的例子,來做下演示:
加入你有一個類Animal,打算對裡邊的所有方法進行包裝,由於這個類沒有實現接口,所以你無法使用jdk 動態代理
public class Animal { public void bark() { System.out.println("i am 大黃!"); } public String singSong(String name) { return name + " is singing!"; } public String testt(String name) { return name + " is testing!"; } }
你現在想要在testt方法輸出前後不加任何內容,但是另外兩個方法輸出前後要加一個字符串,效果如下:
BEFORE
i am 大黃!
AFTER
===============================
BEFORE
老劉 is singing!
AFTER
===============================
大黃 is testing!
其中=======是分隔線,上邊兩個方法前後都改變了,但是最後一個方法則沒做任何改變還是原生的。如何做到?
首先我們要定義一個攔截器,該攔截器實現了 cgLib的MethodInterceptor,如下:
public class AnimalWrapper implements MethodInterceptor { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("BEFORE"); Object obj2 = proxy.invokeSuper(obj, args); System.out.println("AFTER"); return obj2; } }
再定義一個攔截器過濾器,如下:
public class ApiFilter implements CallbackFilter { @Override public int accept(Method method) { String name = method.getName(); if (name.length() == 5) { return 1; } return 0; } }
接下來看下測試類:
public class CglibTest { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Animal.class); enhancer.setCallbacks(new Callback[]{new AnimalWrapper(), NoOp.INSTANCE}); enhancer.setCallbackFilter(new ApiFilter()); Animal animal = (Animal) enhancer.create(); animal.bark(); System.out.println("==============================="); animal.singSong("老劉"); System.out.println("==============================="); animal.testt("大黃"); } }
CGLib通過 Enhancer、Callback、CallbackFilter就可以實現上述功能了。
深入代碼:Enhancer → KeyFactory.Generator → AbstractClassGenerator → DefaultGeneratorStrategy.generate → KeyFactory.Generator.generateClass → ClassWriter.toByteArray 生成 class bytecode .
生成的代碼反編譯如下:
public class Animal$$EnhancerByCGLIB$$223151cf extends com.shock.base.proxy.cglib.Animal implements net.sf.cglib.proxy.Factory { private boolean CGLIB$BOUND; public static java.lang.Object CGLIB$FACTORY_DATA; private static final java.lang.ThreadLocal CGLIB$THREAD_CALLBACKS; private static final net.sf.cglib.proxy.Callback[] CGLIB$STATIC_CALLBACKS; private net.sf.cglib.proxy.MethodInterceptor CGLIB$CALLBACK_0; private net.sf.cglib.proxy.NoOp CGLIB$CALLBACK_1; private static java.lang.Object CGLIB$CALLBACK_FILTER; private static final java.lang.reflect.Method CGLIB$bark$0$Method; private static final net.sf.cglib.proxy.MethodProxy CGLIB$bark$0$Proxy; private static final java.lang.Object[] CGLIB$emptyArgs; private static final java.lang.reflect.Method CGLIB$singSong$1$Method; private static final net.sf.cglib.proxy.MethodProxy CGLIB$singSong$1$Proxy; private static final java.lang.reflect.Method CGLIB$equals$3$Method; private static final net.sf.cglib.proxy.MethodProxy CGLIB$equals$3$Proxy; private static final java.lang.reflect.Method CGLIB$toString$4$Method; private static final net.sf.cglib.proxy.MethodProxy CGLIB$toString$4$Proxy; private static final java.lang.reflect.Method CGLIB$hashCode$5$Method; private static final net.sf.cglib.proxy.MethodProxy CGLIB$hashCode$5$Proxy; static void CGLIB$STATICHOOK1() { /* compiled code */ } final void CGLIB$bark$0() { /* compiled code */ } public final void bark() { /* compiled code */ } final void CGLIB$singSong$1(java.lang.String s) { /* compiled code */ } public final void singSong(java.lang.String s) { /* compiled code */ } final boolean CGLIB$equals$3(java.lang.Object o) { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } final java.lang.String CGLIB$toString$4() { /* compiled code */ } public final java.lang.String toString() { /* compiled code */ } final int CGLIB$hashCode$5() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public static net.sf.cglib.proxy.MethodProxy CGLIB$findMethodProxy(net.sf.cglib.core.Signature signature) { /* compiled code */ } public Animal$$EnhancerByCGLIB$$223151cf() { /* compiled code */ } public static void CGLIB$SET_THREAD_CALLBACKS(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } public static void CGLIB$SET_STATIC_CALLBACKS(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } private static final void CGLIB$BIND_CALLBACKS(java.lang.Object o) { /* compiled code */ } public java.lang.Object newInstance(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } public java.lang.Object newInstance(net.sf.cglib.proxy.Callback callback) { /* compiled code */ } public java.lang.Object newInstance(java.lang.Class[] classes, java.lang.Object[] objects, net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } public net.sf.cglib.proxy.Callback getCallback(int i) { /* compiled code */ } public void setCallback(int i, net.sf.cglib.proxy.Callback callback) { /* compiled code */ } public net.sf.cglib.proxy.Callback[] getCallbacks() { /* compiled code */ } public void setCallbacks(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } } View Code該段代碼最後實在沒辦法生成,是將CGLib代碼源碼下載,然後插入片段代碼生成的。