Java设计模式之代理模式
Posted ZK_小姜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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关键字。
以上是关于Java设计模式之代理模式的主要内容,如果未能解决你的问题,请参考以下文章