Java設計模式之代理模式

分類

從分類上來說,代理模式分爲靜態代理和動態代理兩種:
靜態代理模式:需要人工編寫代理類,也就是在運行時已經存在代理類的class文件了,依賴目標類,代理類和目標類都實現同一接口類,調用時直接訪問代理類,通過代理類將請求轉化爲訪問目標類;
動態代理模式:顧名思義,在運行時動態生成代理類,Spring的aop中就是使用的動態代理,當然動態代理又分爲兩種,一種是jdk實現的動態代理,這種方式只能用於實現了接口的類,如果目標類沒有實現某一接口,則不能生成代理類,因爲生成的代理類需要實現目標接口,繼承Proxy;另外一種則是cglib實現的動態代理,這種方式不管目標類有沒有實現接口,都可以生成代理類,得到的代理類繼承於目標類。Spring的aop中使用了這兩種代理模式。

靜態代理模式

目標接口:

public interface Subject {

    void doSomething();
}

目標類:

public class RealSubject implements Subject {

    @Override
    public void doSomething() {

        System.out.println("doing.....");

    }
}

代理類:

public class ProxySubject implements Subject{

    private Subject target;

    public ProxySubject(Subject target) {
        this.target = target;
    }

    @Override
    public void doSomething() {
        System.out.println("do before...");
        target.doSomething();
        System.out.println("do after...");
    }   
}

測試類:

public class Client {

    public static void main(String[] args) {
        Subject target = new RealSubject();
        Subject proxySubject = new ProxySubject(target);
        proxySubject.doSomething();
    }
}

類圖:
這裏寫圖片描述

動態代理模式(jdk實現)

動態代理的核心是InvocationHandler和Proxy,需要編寫一個handler實現InvocationHandler接口,同時依賴目標類,在handler的invoke方法中編寫代理邏輯,最後利用Proxy生成代理類,繼承Proxy,實現目標接口,同時依賴handler,因此,可以將生成的代理類強制轉化爲目標接口的一個實例,在執行目標方法時,通過代理類轉化爲運行handler的invoke方法。
目標接口:

public interface Subject {

    void doSomething();
}

目標類:

public class RealSubject implements Subject {

    @Override
    public void doSomething() {

        System.out.println("doing.....");

    }
}

調用處理類(handler):

public class Handler implements InvocationHandler {

    private Object target;

    public Handler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println("do before...");
        Object object = method.invoke(target, args);
        System.out.println("do after...");
        return object;
    }

}

測試類:

public class Client {

    public static void main(String[] args) {
        Subject target = new RealSubject();
        Subject subject = (Subject)Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new Handler(target));
        subject.doSomething();
    }
}

類圖:
這裏寫圖片描述

在測試類中添加sun.misc.ProxyGenerator.saveGeneratedFiles設置爲True可以保存生成的代理類class文件,目錄在當前項目的com/sun/proxy下面,使用反編譯軟件就可以查看生成的代理類源代碼了:

public class Client {

    public static void main(String[] args) {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        Subject target = new RealSubject();
        Subject subject = (Subject)Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new Handler(target));
        subject.doSomething();
    }
}

生成的代理類源代碼:

public final class $Proxy0 extends Proxy
implements Subject
{
private static Method m1;
private static Method m3;
private static Method m2;
private static Method m0;

public $Proxy0(InvocationHandler paramInvocationHandler)
  throws 
{
  super(paramInvocationHandler);
}

public final boolean equals(Object paramObject)
  throws 
{
  try
  {
    return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
  }
  catch (Error|RuntimeException localError)
  {
    throw localError;
  }
  catch (Throwable localThrowable)
  {
    throw new UndeclaredThrowableException(localThrowable);
  }
}

public final void doSomething()
  throws 
{
  try
  {
    this.h.invoke(this, m3, null);
    return;
  }
  catch (Error|RuntimeException localError)
  {
    throw localError;
  }
  catch (Throwable localThrowable)
  {
    throw new UndeclaredThrowableException(localThrowable);
  }
}

public final String toString()
  throws 
{
  try
  {
    return (String)this.h.invoke(this, m2, null);
  }
  catch (Error|RuntimeException localError)
  {
    throw localError;
  }
  catch (Throwable localThrowable)
  {
    throw new UndeclaredThrowableException(localThrowable);
  }
}

public final int hashCode()
  throws 
{
  try
  {
    return ((Integer)this.h.invoke(this, m0, null)).intValue();
  }
  catch (Error|RuntimeException localError)
  {
    throw localError;
  }
  catch (Throwable localThrowable)
  {
    throw new UndeclaredThrowableException(localThrowable);
  }
}

static
{
  try
  {
    m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
    m3 = Class.forName("com.ipr.cost.test.Subject").getMethod("doSomething", new Class[0]);
    m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
    m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
    return;
  }
  catch (NoSuchMethodException localNoSuchMethodException)
  {
    throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
  }
  catch (ClassNotFoundException localClassNotFoundException)
  {
    throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
  }
}
}

從源代碼中可以看出:
1、動態生成的代理類繼承了Proxy,實現了Subject接口;
2、構造方法中會傳入InvocationHandler,因此,在初始化代理類的時候,會傳入我們編寫的handler,然後在調用目標接口方法時,會轉化爲handler的invoke方法,進而執行代理的處理邏輯;
3、因爲這裏代理類繼承了Proxy類,又加上java單繼承的特性,這種動態代理只能通過實現接口的方式來完成代理,所以這種代理模式只能基於實現了接口的類。

動態代理模式(cglib實現)

目標接口類:

public interface Subject {

    void doSomething();
}

目標類:

public class RealSubject implements Subject {

    @Override
    public void doSomething() {

        System.out.println("doing.....");

    }
}

cglib攔截類:

public class RealSubjectCglib implements MethodInterceptor{

    private Object target;

    public Object getInstance(Object obj){
        this.target = obj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object arg0, Method arg1, Object[] arg2,
            MethodProxy arg3) throws Throwable {
        System.out.println("do before...");
        Object object = arg3.invokeSuper(arg0, arg2);
        System.out.println("do after...");
        return object;
    }

}

測試類:

public class Client {

    public static void main(String[] args) {
        Subject target = new RealSubject();
        Subject subject = (Subject)new RealSubjectCglib().getInstance(target);
        subject.doSomething();
    }
}

如果要查看動態生成的代理類源碼,可以在測試類中加如下配置:

public class Client {

    public static void main(String[] args) {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\class"); 
        Subject target = new RealSubject();
        Subject subject = (Subject)new RealSubjectCglib().getInstance(target);
        subject.doSomething();
    }
}

生成的代理類源碼:

public class RealSubject$$EnhancerByCGLIB$$91fb6d63 extends RealSubject
  implements Factory
{
  private boolean CGLIB$BOUND;
  private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
  private static final Callback[] CGLIB$STATIC_CALLBACKS;
  private MethodInterceptor CGLIB$CALLBACK_0;
  private static final Method CGLIB$doSomething$0$Method;
  private static final MethodProxy CGLIB$doSomething$0$Proxy;
  private static final Object[] CGLIB$emptyArgs;
  private static final Method CGLIB$finalize$1$Method;
  private static final MethodProxy CGLIB$finalize$1$Proxy;
  private static final Method CGLIB$equals$2$Method;
  private static final MethodProxy CGLIB$equals$2$Proxy;
  private static final Method CGLIB$toString$3$Method;
  private static final MethodProxy CGLIB$toString$3$Proxy;
  private static final Method CGLIB$hashCode$4$Method;
  private static final MethodProxy CGLIB$hashCode$4$Proxy;
  private static final Method CGLIB$clone$5$Method;
  private static final MethodProxy CGLIB$clone$5$Proxy;

  static void CGLIB$STATICHOOK1()
  {
    CGLIB$THREAD_CALLBACKS = new ThreadLocal();
    CGLIB$emptyArgs = new Object[0];
    Class localClass1 = Class.forName("com.ipr.cost.test.RealSubject$$EnhancerByCGLIB$$91fb6d63");
    Class localClass2;
    Method[] tmp95_92 = ReflectUtils.findMethods(new String[] { "finalize", "()V", "equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;" }, (localClass2 = Class.forName("java.lang.Object")).getDeclaredMethods());
    CGLIB$finalize$1$Method = tmp95_92[0];
    CGLIB$finalize$1$Proxy = MethodProxy.create(localClass2, localClass1, "()V", "finalize", "CGLIB$finalize$1");
    Method[] tmp115_95 = tmp95_92;
    CGLIB$equals$2$Method = tmp115_95[1];
    CGLIB$equals$2$Proxy = MethodProxy.create(localClass2, localClass1, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
    Method[] tmp135_115 = tmp115_95;
    CGLIB$toString$3$Method = tmp135_115[2];
    CGLIB$toString$3$Proxy = MethodProxy.create(localClass2, localClass1, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
    Method[] tmp155_135 = tmp135_115;
    CGLIB$hashCode$4$Method = tmp155_135[3];
    CGLIB$hashCode$4$Proxy = MethodProxy.create(localClass2, localClass1, "()I", "hashCode", "CGLIB$hashCode$4");
    Method[] tmp175_155 = tmp155_135;
    CGLIB$clone$5$Method = tmp175_155[4];
    CGLIB$clone$5$Proxy = MethodProxy.create(localClass2, localClass1, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
    tmp175_155;
    Method[] tmp223_220 = ReflectUtils.findMethods(new String[] { "doSomething", "()V" }, (localClass2 = Class.forName("com.ipr.cost.test.RealSubject")).getDeclaredMethods());
    CGLIB$doSomething$0$Method = tmp223_220[0];
    CGLIB$doSomething$0$Proxy = MethodProxy.create(localClass2, localClass1, "()V", "doSomething", "CGLIB$doSomething$0");
    tmp223_220;
    return;
  }

  final void CGLIB$doSomething$0()
  {
    super.doSomething();
  }

  public final void doSomething()
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    if (this.CGLIB$CALLBACK_0 != null)
      return;
    super.doSomething();
  }

  final void CGLIB$finalize$1()
    throws Throwable
  {
    super.finalize();
  }

  protected final void finalize()
    throws Throwable
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    if (this.CGLIB$CALLBACK_0 != null)
      return;
    super.finalize();
  }

  final boolean CGLIB$equals$2(Object paramObject)
  {
    return super.equals(paramObject);
  }

  public final boolean equals(Object paramObject)
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
    {
      Object tmp41_36 = tmp17_14.intercept(this, CGLIB$equals$2$Method, new Object[] { paramObject }, CGLIB$equals$2$Proxy);
      tmp41_36;
      return tmp41_36 == null ? false : ((Boolean)tmp41_36).booleanValue();
    }
    return super.equals(paramObject);
  }

  final String CGLIB$toString$3()
  {
    return super.toString();
  }

  public final String toString()
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
      return (String)tmp17_14.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy);
    return super.toString();
  }

  final int CGLIB$hashCode$4()
  {
    return super.hashCode();
  }

  public final int hashCode()
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
    {
      Object tmp36_31 = tmp17_14.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);
      tmp36_31;
      return tmp36_31 == null ? 0 : ((Number)tmp36_31).intValue();
    }
    return super.hashCode();
  }

  final Object CGLIB$clone$5()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  protected final Object clone()
    throws CloneNotSupportedException
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
      return tmp17_14.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy);
    return super.clone();
  }

  public static MethodProxy CGLIB$findMethodProxy(Signature paramSignature)
  {
    String tmp4_1 = paramSignature.toString();
    switch (tmp4_1.hashCode())
    {
    case -1574182249:
      if (tmp4_1.equals("finalize()V"))
        return CGLIB$finalize$1$Proxy;
      break;
    case -508378822:
    case 1826985398:
    case 1913648695:
    case 1984935277:
    case 2121560294:
    }
  }

  public RealSubject$$EnhancerByCGLIB$$91fb6d63()
  {
    CGLIB$BIND_CALLBACKS(this);
  }

  public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] paramArrayOfCallback)
  {
    CGLIB$THREAD_CALLBACKS.set(paramArrayOfCallback);
  }

  public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] paramArrayOfCallback)
  {
    CGLIB$STATIC_CALLBACKS = paramArrayOfCallback;
  }

  private static final void CGLIB$BIND_CALLBACKS(Object paramObject)
  {
    // Byte code:
    //   0: aload_0
    //   1: checkcast 2 com/ipr/cost/test/RealSubject$$EnhancerByCGLIB$$91fb6d63
    //   4: astore_1
    //   5: aload_1
    //   6: getfield 198    com/ipr/cost/test/RealSubject$$EnhancerByCGLIB$$91fb6d63:CGLIB$BOUND    Z
    //   9: ifne +43 -> 52
    //   12: aload_1
    //   13: iconst_1
    //   14: putfield 198   com/ipr/cost/test/RealSubject$$EnhancerByCGLIB$$91fb6d63:CGLIB$BOUND    Z
    //   17: getstatic 24   com/ipr/cost/test/RealSubject$$EnhancerByCGLIB$$91fb6d63:CGLIB$THREAD_CALLBACKS Ljava/lang/ThreadLocal;
    //   20: invokevirtual 201  java/lang/ThreadLocal:get   ()Ljava/lang/Object;
    //   23: dup
    //   24: ifnonnull +15 -> 39
    //   27: pop
    //   28: getstatic 196  com/ipr/cost/test/RealSubject$$EnhancerByCGLIB$$91fb6d63:CGLIB$STATIC_CALLBACKS [Lorg/springframework/cglib/proxy/Callback;
    //   31: dup
    //   32: ifnonnull +7 -> 39
    //   35: pop
    //   36: goto +16 -> 52
    //   39: checkcast 202  [Lorg/springframework/cglib/proxy/Callback;
    //   42: aload_1
    //   43: swap
    //   44: iconst_0
    //   45: aaload
    //   46: checkcast 48   org/springframework/cglib/proxy/MethodInterceptor
    //   49: putfield 36    com/ipr/cost/test/RealSubject$$EnhancerByCGLIB$$91fb6d63:CGLIB$CALLBACK_0   Lorg/springframework/cglib/proxy/MethodInterceptor;
    //   52: return
  }

  public Object newInstance(Callback[] paramArrayOfCallback)
  {
    CGLIB$SET_THREAD_CALLBACKS(paramArrayOfCallback);
    CGLIB$SET_THREAD_CALLBACKS(null);
    return new 91fb6d63();
  }

  public Object newInstance(Callback paramCallback)
  {
    CGLIB$SET_THREAD_CALLBACKS(new Callback[] { paramCallback });
    CGLIB$SET_THREAD_CALLBACKS(null);
    return new 91fb6d63();
  }

  public Object newInstance(Class[] paramArrayOfClass, Object[] paramArrayOfObject, Callback[] paramArrayOfCallback)
  {
    CGLIB$SET_THREAD_CALLBACKS(paramArrayOfCallback);
    Class[] tmp9_8 = paramArrayOfClass;
    switch (tmp9_8.length)
    {
    case 0:
      tmp9_8;
      break;
    default:
      new 91fb6d63();
      throw new IllegalArgumentException("Constructor not found");
    }
    CGLIB$SET_THREAD_CALLBACKS(null);
  }

  public Callback getCallback(int paramInt)
  {
    CGLIB$BIND_CALLBACKS(this);
    switch (paramInt)
    {
    case 0:
      break;
    }
    return null;
  }

  public void setCallback(int paramInt, Callback paramCallback)
  {
    switch (paramInt)
    {
    case 0:
      this.CGLIB$CALLBACK_0 = ((MethodInterceptor)paramCallback);
      break;
    }
  }

  public Callback[] getCallbacks()
  {
    CGLIB$BIND_CALLBACKS(this);
    return new Callback[] { this.CGLIB$CALLBACK_0 };
  }

  public void setCallbacks(Callback[] paramArrayOfCallback)
  {
    this.CGLIB$CALLBACK_0 = ((MethodInterceptor)paramArrayOfCallback[0]);
  }

  static
  {
    CGLIB$STATICHOOK1();
  }
}

從源碼中可以看出:
1、生成的代理類繼承了目標類,實現了Factory接口;
2、在攔截類中通過構造方法引入了目標類,在生成代理類的時候,設置了回調類爲當前的攔截類,所以每當調用代理類的目標接口方法時,就會觸發攔截,進而順勢織入橫切邏輯;
3、代理類是通過繼承目標類來完成的代理,所以目標類不需要實現接口也可以使用這種代理模式,當然,實現了接口也不影響;
4、因爲是通過繼承來實現的代理,所以目標類和類中的方法都不能使用final關鍵字。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章