日常生活中代理司空见惯的,比如某地区总代理,直白点就是中间商。在代码的世界里也有这种“中间商”,不过这个“中间商”不收差价,有时候甚至给你点额外的“小惊喜”。
代理模式有3个要素:接口、目标类(委托人)、代理类(中间商)。目标类、代理类都实现同一个接口,代理类有一个目标类的属性。客户所有的请求都是到代理类对象,代理对象转发到目标对象。
那么前面说的“小惊喜”是什么呢? 代理对象在找目标对象之前,可以自己做一些扩展。
所以代理模式有以下好处:
一般情况要实现代理模式,开发者需要显示创建代理类,然后编译打包运行,我们称为静态代理。不过,由于jvm类加载的机制,允许开发者主动加载某个类,开发者可以在程序运行时动态创建代理类,然后通过类加载器加载到jvm,实例化代理对象,这就是动态代理。动态代理的核心是如何生成代理类。目前,有两种方式生成动态代理:JDK动态代理、CGLIB动态代理。
jdk动态代理是jdk自带的动态代理方式,核心组件有java.lang.reflect.Proxy、java.lang.reflect.InvocationHandler(接口)。jdk动态代理稍微修改了代理模式模型-引入了InvocationHandler。jdk动态代理生成代理类之后的类图模型如下:
jdk生成的代理类ProxyClass继承java.lang.reflect.Proxy,后者有个成员变量h(java.lang.reflect.InvocationHandler),开发者可以在InvocationHandler实现类中关联目标对象(TargetClass)。创建代理对象的实现需要传入InvocationHandler的实现类。引入InvocationHandler有两点作用:
jdk动态代理使用起来很简单,直接拿例子说吧。如下图,假设有LocalWriterPrinter实现3个接口Priter、Writer、WriterPrinter,我们对目标对象(LocalWriterPrinter)做动态代理,在方法前后加上额外的信息。
目标类LocalWriterPrinter实现3个接口:
public class LocalWriterPrinter implements Writer, Printer, WriterPrinter {
public String print(String text) {
System.out.println(this.getClass().getSimpleName() + " print:" + text);
return text;
}
public String write(String text) {
System.out.println(this.getClass().getSimpleName() + " write:" + text);
return text;
}
}
Jdk动态代理是通过InvocationHandler#invoke分发代理请求,所以需要一个InvocationHandler实现类WriterPrinterInvocationHandler,目标类对象printer是其成员变量。我们在调用目标类对象之前、之后还有finlly都做一个额外的事情(例子只打个日志)
public class WriterPrinterInvocationHandler implements InvocationHandler {
private Object printer;
public WriterPrinterInvocationHandler(Object writerPrinter) {
this.printer = writerPrinter;
};
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
System.out.println("before advice:" + method.getDeclaringClass().getSimpleName() + "." + method.getName());
Object ret = method.invoke(printer, args);
System.out.println("after advice:" + method.getDeclaringClass().getSimpleName() + "." + method.getName());
return ret;
} finally {
System.out.println("finally advice:" + method.getDeclaringClass().getSimpleName() + "." + method.getName());
}
}
}
public class JdkDynamicDemo {
public static void main(String... args) {
//创建目标类对象
LocalWriterPrinter target= new LocalWriterPrinter();
//创建InvocationHander,并将目标类对象作为属性
WriterPrinterInvocationHandler handler = new WriterPrinterInvocationHandler(target);
Class>[] infs = {Printer.class, WriterPrinter.class, Writer.class};
//调用Proxy.newProxyInstance创建代理类对象
Object writerPrinter = Proxy.newProxyInstance(JdkDynamicDemo.class.getClassLoader(), infs, handler);
//执行代理类对象的print方法
Printer printer = (Printer)writerPrinter;
printer.print("jdk dynamic");
//执行代理类对象的write方法
Writer writer = (Writer) writerPrinter;
writer.write("jdk dynamic");
}
}
运行结果如下:
我们看以下生成的代理类是什么样的(用jdk的HSDB工具导出代理类)
package com.sun.proxy;
import com.focuse.dynamicdemo.jdkproxy.Printer;
import com.focuse.dynamicdemo.jdkproxy.Writer;
import com.focuse.dynamicdemo.jdkproxy.WriterPrinter;
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 Printer, WriterPrinter, Writer {
private static Method m1;
private static Method m2;
private static Method m0;
private static Method m3;
private static Method m4;
public $Proxy0(InvocationHandler var1) {
super(var1);
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
m3 = Class.forName("com.focuse.dynamicdemo.jdkproxy.Printer").getMethod("print", Class.forName("java.lang.String"));
//笔者注:write方法来源于WritePrinter,因为传入的接口数组中WritePrinter接口在前,Writer接口在后
m4 = Class.forName("com.focuse.dynamicdemo.jdkproxy.WriterPrinter").getMethod("write", Class.forName("java.lang.String"));
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
public final boolean equals(Object var1) {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() {
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 int hashCode() {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final String write(String var1) {
try {
return (String)super.h.invoke(this, m4, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String print(String var1) {
try {
return (String)super.h.invoke(this, m3, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
}
h就是创建代理对象时传入的InvocationHandler实例,看到代理类的代码一切都清晰了,所的请求的都转到InvocationHandler实例h处理。InvocationHandler#invoke第一个参数是代理对象,第二个参数是被代理接口的method,第三个参数请求的实际参数。显而易见,jdk动态代理有以下特点:
不同于JDK动态代理,CGLIB是通过生成目标类的子类实现的。核心组件是net.sf.cglib.proxy.Enhancer和net.sf.cglib.proxy.Callback。Callback相当于JDK动态代理中的InvocationHandler。其实,CGLIB的功能只是增强父类的逻辑,要用它实现代理模式,需要在Callback里关联目标对象。子类的逻辑是如果Callback不为空执行Callback,否则执行父类方法。
CGLIB提供四种Callback子类型,一般做动态代理的是net.sf.cglib.proxy.MethodInterceptor:
咋一看这个类图看不出是代理模式,我们转换一下画一个对象图就看得明白了。我们用黑心圆点表示实例关系,如下图,代理对象和目标对象都是目标类的实例,代理对象实际是目标类子类(代理类)的实例。
同样,我们举个小栗子,对Printer类做动态代理,并在print方法之前、之后、finally位置做一些扩展(本例仅仅打个日志)。首先看一下原来的Printer类
public class Printer {
public String print(String text) {
System.out.println("Local Printer:" + text);
return text;
}
protected String print22(String text) {
System.out.println("Local Printer:" + text);
return text;
}
private String print33(String text) {
System.out.println("Local Printer:" + text);
return text;
}
}
其次,实现一个MethodInterceptor,目标对象作为其成员变量,并在调用目标对象方法之前、之后、finally位置加上相应日志。
public class LocalPrinterCallback implements MethodInterceptor {
Object target;
public LocalPrinterCallback(Object target) {
this.target = target;
}
//代理
public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,
MethodProxy proxy) throws Throwable{
try {
System.out.println("before advice");
//请求目标对象方法
Object ret = method.invoke(target, args);
System.out.println("after advice");
return ret;
}finally {
System.out.println("finally advice");
}
}
}
接下来就是开始用CGLIB进行动态代理
public class CglibDynamicDemo {
public static void main(String... args) {
//目标对象
Printer target = new Printer();
//callback 目标对象作为其成员变量
LocalPrinterCallback localCallback = new LocalPrinterCallback(target);
//创建enhancer类
Enhancer enhancer = new Enhancer();
//设置需要增强的类Printer
enhancer.setSuperclass(Printer.class);
//设置callback 相当于jdk动态代理的InvocationHandler
enhancer.setCallback(localCallback);
//创建代理类并返回代理对象
Object object = enhancer.create();
Printer proxy = (Printer)object;
//执行print方法
proxy.print("cglib demo");
}
}
运行结果如下,期望在增强的位置打出相应的信息:
我们看一下CGLIB生成的代理类是什么样的(也是用jdk的HSDB工具导出代理类) 。cglib生成的代理类比jdk动态代理生成的代理类相对复杂一些。不过,我们主要看一下print方法的实现,不难看出,print方法的逻辑是当callback不为空null时就去调用callback的net.sf.cglib.proxy.MethodInterceptor#intercept方法(本例中callback是MethodInterceptor类型)。同时,我们也看到cglib只对public和protected修饰的方法做代理,private方法则不做。
package com.focuse.dynamicdemo.cglibproxy;
import java.lang.reflect.Method;
import net.sf.cglib.core.ReflectUtils;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class Printer$$EnhancerByCGLIB$$9402db17 extends Printer implements Factory {
private boolean CGLIB$BOUND;
public static Object CGLIB$FACTORY_DATA;
private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final Callback[] CGLIB$STATIC_CALLBACKS;
private MethodInterceptor CGLIB$CALLBACK_0;
private static Object CGLIB$CALLBACK_FILTER;
private static final Method CGLIB$print$0$Method;
private static final MethodProxy CGLIB$print$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$print22$1$Method;
private static final MethodProxy CGLIB$print22$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;
public Printer$$EnhancerByCGLIB$$9402db17() {
CGLIB$BIND_CALLBACKS(this);
}
static {
CGLIB$STATICHOOK1();
}
......(略)
public final String print(String var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$print$0$Method, new Object[]{var1}, CGLIB$print$0$Proxy) : super.print(var1);
}
protected final String print22(String var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$print22$1$Method, new Object[]{var1}, CGLIB$print22$1$Proxy) : super.print22(var1);
}
......(略)
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.focuse.dynamicdemo.cglibproxy.Printer$$EnhancerByCGLIB$$9402db17");
Class var1;
Method[] var10000 = ReflectUtils.findMethods(new String[]{"print", "(Ljava/lang/String;)Ljava/lang/String;", "print22", "(Ljava/lang/String;)Ljava/lang/String;"}, (var1 = Class.forName("com.focuse.dynamicdemo.cglibproxy.Printer")).getDeclaredMethods());
CGLIB$print$0$Method = var10000[0];
CGLIB$print$0$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)Ljava/lang/String;", "print", "CGLIB$print$0");
CGLIB$print22$1$Method = var10000[1];
CGLIB$print22$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)Ljava/lang/String;", "print22", "CGLIB$print22$1");
var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
CGLIB$equals$2$Method = var10000[0];
CGLIB$equals$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
CGLIB$toString$3$Method = var10000[1];
CGLIB$toString$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
CGLIB$hashCode$4$Method = var10000[2];
CGLIB$hashCode$4$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$4");
CGLIB$clone$5$Method = var10000[3];
CGLIB$clone$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
}
final String CGLIB$print$0(String var1) {
return super.print(var1);
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
Printer$$EnhancerByCGLIB$$9402db17 var1 = (Printer$$EnhancerByCGLIB$$9402db17)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (CGLIB$STATIC_CALLBACKS == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
final String CGLIB$print22$1(String var1) {
return super.print22(var1);
}
}
这里介绍代理模式、动态代理以及动态代理的两种方式:JDK自带方式动态代理、CGLIB创建动态代理,总结一下