代理是一种设计模式,目的是对外提供统一的接口(这样可以隐藏被代理类不想公开的方法),在此基础上,可以在代理类实现逻辑中加入一些附加操作,做一些增强处理,该方式不会影响被代理类的原实现。
代理分为静态代理和动态代理两种,静态代理就是在代理类内部持有被代理对象的引用,这种方式很直接,但需要hard coding,不易扩展,无法做统一处理。
动态代理主要有jdk提供的Proxy、Cglib以及Javassist,其中Javassist主要是一套字节码操作库,有两种使用方式,既可以有类似Cglib的用法,也可以根据字节码结构去动态生成类,本文主要讲述Proxy和Cglib。
动态代理在Spring、Mybatis、Dubbo等各大框架中均有大量使用,因此熟悉动态代理是阅读这些框架源码必须掌握的基本功之一
Proxy是根据接口生成代理类,生成的代理类实现了被代理类的接口,Cglib是生成被代理类的子类,因此被代理类不能为final,而且必须有无参构造函数。
Proxy
Proxy需要写一个实现InvocationHandler(实现invoke方法)的代理类(以下称Agency),注意Agency不是最后生成的动态代理类,动态代理类是Proxy.newProxyInstance过程中生成的,newProxyInstance方法会为动态代理类newInstance一个实例对象,动态代理类的主要思想是调用agency 对象(Agency类的实例)的invoke方法,在invoke方法中再通过反射调用被代理对象的方法,增加附加处理。
上代码:
interface Subject {
void doSomething();
}
interface DupSubject {
void doSomethingAgain();
}
class Real implements Subject, DupSubject {
public void doSomething() {
System.out.println("=========Real doSomething=========");
}
@Override
public void doSomethingAgain() {
System.out.println("=========Real doSomethingAgain=========");
}
}
class Agency implements InvocationHandler {
private Real sub;
public Object bind(Real sub) {
this.sub = sub;
Object obj = Proxy.newProxyInstance(Test.class.getClassLoader(), sub.getClass().getInterfaces(), this);
return obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 前置增强处理
System.out.println("+++++++++Agency begin+++++++");
method.invoke(sub, args);
// 后置增强处理
System.out.println("+++++++++Agency end+++++++");
return null;
}
}
public class Test {
public static void main(String[] args) {
// 设置系统参数,输出动态生成的代理类
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Agency agency = new Agency();
Real real = new Real();
// 绑定被代理对象,返回代理对象
Object obj = agency.bind(real);
Subject sub = (Subject) (obj);
sub.doSomething();
DupSubject dubSub = (DupSubject) (obj);
dubSub.doSomethingAgain();
}
}
程序输出内容为:
+++++++++Agency begin+++++++
=========Real doSomething=========
+++++++++Agency end+++++++
+++++++++Agency begin+++++++
=========Real doSomethingAgain=========
+++++++++Agency end+++++++
上生成的动态代理类:
// 默认继承Proxy类且实现被代理类的所有接口
final class $Proxy0 extends Proxy implements Subject, DupSubject {
private static Method m1;
private static Method m3;
private static Method m2;
private static Method m4;
private static Method m0;
// 将所有method对象在初始加载代理类时缓存下来,防止每次都通过反射获取
static {
try {
m3 = Class.forName("Subject").getMethod("doSomething", new Class[0]);
m4 = Class.forName("DupSubject").getMethod("doSomethingAgain", new Class[0]);
// 省略toString/equals/hashcode等方法
...
} catch (NoSuchMethodException nosuchmethodexception) {
throw new NoSuchMethodError(nosuchmethodexception.getMessage());
} catch (ClassNotFoundException classnotfoundexception) {
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
}
public $Proxy0(InvocationHandler invocationhandler) {
// 向super类也就是proxy类传入InvocationHandler对象
super(invocationhandler);
}
// 直接调用了之前传入的InvocationHandler对象的invoke方法
public final void doSomething() {
try {
super.h.invoke(this, m3, null);
return;
} catch (Error) {
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
// 直接调用了之前传入的InvocationHandler对象的invoke方法
public final void doSomethingAgain() {
try {
super.h.invoke(this, m4, null);
return;
} catch (Error) {
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
}
动态代理类是如何生成的具体参照ProxyGenerator的 generateClassFile方法,该方法先为代理类添加equals/hashcode/toString方法,然后增加被代理类接口的所有方法,处理过程主要为通过反射获取被代理方法的方法名、参数类型、返回值类型、异常类型,然后生成根据以上参数生成代理方法的字节码内容(字节码实现一个事:即调用属性InvocationHandler对象的invoke方法),看这个方法可以充分理解class文件的组成,即常量池、字段表、方法表等。
注意:proxy方法之间的内部调用是不走invoke逻辑的
Cglib
上代码:
interface Subject {
void doSomething();
}
interface AnotherSubject {
void doSomethingAgain();
}
class Real implements Subject, AnotherSubject {
@Override
public void doSomething() {
System.out.println("=========Real doSomething=========");
}
@Override
public void doSomethingAgain() {
System.out.println("=========Real doSomethingAgain begin====");
// 调用内部方法,注意invokeSuper时该调用是否会进入增强逻辑
this.doSomething();
System.out.println("=========Real doSomethingAgain end====");
}
}
class Agency implements MethodInterceptor {
private Object target;
// 持有被代理对象
public Agency(Object target) {
this.target = target;
}
@Override
// obj对象为动态代理类的实例
// method为被代理类的方法
// methodProxy持有被代理方法、代理方法、被代理类、代理类以及方法的index索引
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("+++++++++Agency begin+++++++");
// 直接调用被代理对象的方法
method.invoke(target, args);
// 调用methodProxy的invokeSuper方法,注意如果调用methodProxy的invoke方法
// 因为传入的obj为动态代理对象,则会陷入死循环,如果为被代理对象target,则不会
proxy.invokeSuper(obj, args);
System.out.println("+++++++++Agency end+++++++");
return null;
}
}
public class Test {
public static void main(String[] args) {
// 设置系统参数,输出动态生成的代理类
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:/temp/dymamicCode");
Real real = new Real();
Agency agency = new Agency(real);
Enhancer enhancer = new Enhancer();
// 设置被代理类
enhancer.setSuperclass(real.getClass());
// 设置回调MethodInterceptor对象
enhancer.setCallback(agency);
Real proxy = (Real)enhancer.create();
proxy.doSomething();
proxy.doSomethingAgain();
}
}
程序输出结果为:
+++++++++Agency begin+++++++
=========Real doSomething=========
=========Real doSomething=========
+++++++++Agency end+++++++
+++++++++Agency begin+++++++
// method.invoke 内容 begin
=========Real doSomethingAgain begin====
=========Real doSomething=========
=========Real doSomethingAgain end====
// method.invoke 内容 end
// methodProxy invokeSuper 内容 begin
=========Real doSomethingAgain begin====
// 传入的obj是动态代理类实例,因此内部调用的this为动态代理类实例,进入增强逻辑
+++++++++Agency begin+++++++
=========Real doSomething=========
=========Real doSomething=========
+++++++++Agency end+++++++
=========Real doSomethingAgain end====
// methodProxy invokeSuper 内容 end
+++++++++Agency end+++++++
上生成的动态代理类,主要有三个类,在生成fastclass过程中还有一些附加类:
- 生成的动态代理类
// 默认实现Factory接口,继承被代理类
public class Real$$EnhancerByCGLIB$$148ae4e extends Real 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;
// MethodInterceptor对象,即callback
private MethodInterceptor CGLIB$CALLBACK_0;
// 样例没有实现filter,通过filter去过滤callback
private static Object CGLIB$CALLBACK_FILTER;
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$doSomethingAgain$1$Method;
private static final MethodProxy CGLIB$doSomethingAgain$1$Proxy;
// 略去equals/hashcode等方法
...
// 为了方便阅读,将该static块提前
static {
CGLIB$STATICHOOK1();
}
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
// var0为代理类
Class var0 = Class.forName("Real$$EnhancerByCGLIB$$148ae4e");
// var1为被代理类
Class var1;
// var10000持有所有的被代理方法
Method[] var10000 = ReflectUtils.findMethods(new String[]{"doSomething", "()V", "doSomethingAgain", "()V"}, (var1 = Class.forName("Real")).getDeclaredMethods());
// CGLIB$doSomething$0$Method为被代理方法doSomething
CGLIB$doSomething$0$Method = var10000[0];
// doSomething的MethodProxy
CGLIB$doSomething$0$Proxy = MethodProxy.create(var1, var0, "()V", "doSomething", "CGLIB$doSomething$0");
CGLIB$doSomethingAgain$1$Method = var10000[1];
CGLIB$doSomethingAgain$1$Proxy = MethodProxy.create(var1, var0, "()V", "doSomethingAgain", "CGLIB$doSomethingAgain$1");
// 略去equals/hashcode等方法
...
}
final void CGLIB$doSomething$0() {
super.doSomething();
}
// 代理方法doSomething调用入口
public final void doSomething() {
// CGLIB$CALLBACK_0为MethodInterceptor对象
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
// 调用MethodInterceptor对象的intercept方法
// 重点看下调用传参:
// this为代理对象实例,即Real$$EnhancerByCGLIB$$148ae4e类的实例
// CGLIB$doSomething$0$Method为被代理方法doSomething
// CGLIB$emptyArgs 无参
// CGLIB$doSomething$0$Proxy为doSomething的methodProxy
var10000.intercept(this, CGLIB$doSomething$0$Method, CGLIB$emptyArgs, CGLIB$doSomething$0$Proxy);
} else {
super.doSomething();
}
}
final void CGLIB$doSomethingAgain$1() {
super.doSomethingAgain();
}
// 代理方法doSomethingAgain调用入口
public final void doSomethingAgain() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
// 参数分析同上
var10000.intercept(this, CGLIB$doSomethingAgain$1$Method, CGLIB$emptyArgs, CGLIB$doSomethingAgain$1$Proxy);
} else {
super.doSomethingAgain();
}
}
// 略去equals/hashcode等方法
...
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case 516651364:
if (var10000.equals("doSomethingAgain()V")) {
return CGLIB$doSomethingAgain$1$Proxy;
}
break;
// 略去equals/hashcode等方法
...
case 2121560294:
if (var10000.equals("doSomething()V")) {
return CGLIB$doSomething$0$Proxy;
}
}
return null;
}
public Real$$EnhancerByCGLIB$$148ae4e() {
CGLIB$BIND_CALLBACKS(this);
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
}
// 设置属性CGLIB$CALLBACK_0,即MethodInterceptor对象
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
Real$$EnhancerByCGLIB$$148ae4e var1 = (Real$$EnhancerByCGLIB$$148ae4e)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];
}
}
// 生成被代理类实例
public Object newInstance(Callback[] var1) {
// 设置MethodInterceptor对象
CGLIB$SET_THREAD_CALLBACKS(var1);
Real$$EnhancerByCGLIB$$148ae4e var10000 = new Real$$EnhancerByCGLIB$$148ae4e();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
Real$$EnhancerByCGLIB$$148ae4e var10000 = new Real$$EnhancerByCGLIB$$148ae4e();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
Real$$EnhancerByCGLIB$$148ae4e var10000 = new Real$$EnhancerByCGLIB$$148ae4e;
switch(var1.length) {
case 0:
var10000.();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
}
public Callback getCallback(int var1) {
CGLIB$BIND_CALLBACKS(this);
MethodInterceptor var10000;
switch(var1) {
case 0:
var10000 = this.CGLIB$CALLBACK_0;
break;
default:
var10000 = null;
}
return var10000;
}
public void setCallback(int var1, Callback var2) {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
default:
}
}
public Callback[] getCallbacks() {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0};
}
public void setCallbacks(Callback[] var1) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
}
}
生成上述class的方式可见Enhancer的generateClass方法
由上可见,最终调用methodInterceptor的intercept方法,该方法中进行增强处理,然后调用被代理对象的相关方法,两种方式:
- method.invoke
- methodProxy.invokeSuper
method.invoke就是直接调用被代理方法,重点看看methodProxy的invokeSuper和invoke会发生什么?
public Object invokeSuper(Object obj, Object[] args) throws Throwable {
try {
// 初始化fastClass信息
this.init();
MethodProxy.FastClassInfo fci = this.fastClassInfo;
// obj为动态代理对象 f2为代理类的fastClass
// i2为该方法在fastClass的index
return fci.f2.invoke(fci.i2, obj, args);
...
}
public Object invoke(Object obj, Object[] args) throws Throwable {
try {
this.init();
MethodProxy.FastClassInfo fci = this.fastClassInfo;
// obj为被代理对象 f1为被代理类的fastClass
// i1为该方法在fastClass的index
return fci.f1.invoke(fci.i1, obj, args);
...
}
都调用了init方法,该方法会初始化fastClassInfo,看看生成的fastClass的格式:
// 为被代理类生成的FastClass
public class Real$$FastClassByCGLIB$$26cdbe extends FastClass {
public Real$$FastClassByCGLIB$$26cdbe(Class var1) {
super(var1);
}
// 根据方法 name和desc获取index
public int getIndex(Signature var1) {
String var10000 = var1.toString();
switch(var10000.hashCode()) {
case 516651364:
if (var10000.equals("doSomethingAgain()V")) {
return 1;
}
break;
...
case 2121560294:
if (var10000.equals("doSomething()V")) {
return 0;
}
}
return -1;
}
public int getIndex(String var1, Class[] var2) {
switch(var1.hashCode()) {
...
case -1265074063:
if (var1.equals("doSomethingAgain")) {
switch(var2.length) {
case 0:
return 1;
}
}
break;
case 1794410543:
if (var1.equals("doSomething")) {
switch(var2.length) {
case 0:
return 0;
}
}
}
return -1;
}
public int getIndex(Class[] var1) {
switch(var1.length) {
case 0:
return 0;
default:
return -1;
}
}
// 根据index var1调用被代理对象var2的方法
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
Real var10000 = (Real)var2;
int var10001 = var1;
try {
switch(var10001) {
case 0:
var10000.doSomething();
return null;
case 1:
var10000.doSomethingAgain();
return null;
...
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
Real var10000 = new Real;
Real var10001 = var10000;
int var10002 = var1;
try {
switch(var10002) {
case 0:
var10001.();
return var10000;
}
} catch (Throwable var3) {
throw new InvocationTargetException(var3);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 4;
}
}
// 为动态代理类生成的fastClass
public class Real$$EnhancerByCGLIB$$148ae4e$$FastClassByCGLIB$$1c2aeb82 extends FastClass {
public Real$$EnhancerByCGLIB$$148ae4e$$FastClassByCGLIB$$1c2aeb82(Class var1) {
super(var1);
}
public int getIndex(Signature var1) {
String var10000 = var1.toString();
switch(var10000.hashCode()) {
case -2055565910:
if (var10000.equals("CGLIB$SET_THREAD_CALLBACKS([Lnet/sf/cglib/proxy/Callback;)V")) {
return 14;
}
break;
case -1457535688:
if (var10000.equals("CGLIB$STATICHOOK1()V")) {
return 21;
}
break;
case -894172689:
if (var10000.equals("newInstance(Lnet/sf/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 6;
}
break;
case -713887907:
if (var10000.equals("CGLIB$doSomething$0()V")) {
return 22;
}
break;
case -623122092:
if (var10000.equals("CGLIB$findMethodProxy(Lnet/sf/cglib/core/Signature;)Lnet/sf/cglib/proxy/MethodProxy;")) {
return 20;
}
break;
case -419626537:
if (var10000.equals("setCallbacks([Lnet/sf/cglib/proxy/Callback;)V")) {
return 10;
}
break;
case 516651364:
if (var10000.equals("doSomethingAgain()V")) {
return 9;
}
break;
case 560567118:
if (var10000.equals("setCallback(ILnet/sf/cglib/proxy/Callback;)V")) {
return 7;
}
break;
case 811063227:
if (var10000.equals("newInstance([Ljava/lang/Class;[Ljava/lang/Object;[Lnet/sf/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 4;
}
break;
case 973717575:
if (var10000.equals("getCallbacks()[Lnet/sf/cglib/proxy/Callback;")) {
return 12;
}
break;
case 1132140820:
if (var10000.equals("CGLIB$doSomethingAgain$1()V")) {
return 15;
}
break;
case 1221173700:
if (var10000.equals("newInstance([Lnet/sf/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 5;
}
break;
case 1230699260:
if (var10000.equals("getCallback(I)Lnet/sf/cglib/proxy/Callback;")) {
return 11;
}
break;
case 1584330438:
if (var10000.equals("CGLIB$SET_STATIC_CALLBACKS([Lnet/sf/cglib/proxy/Callback;)V")) {
return 13;
}
break;
case 2121560294:
if (var10000.equals("doSomething()V")) {
return 8;
}
...
}
return -1;
}
// 根据方法name和desc获取index
public int getIndex(String var1, Class[] var2) {
switch(var1.hashCode()) {
case -1549500735:
if (var1.equals("CGLIB$doSomethingAgain$1")) {
switch(var2.length) {
case 0:
return 15;
}
}
break;
case -1265074063:
if (var1.equals("doSomethingAgain")) {
switch(var2.length) {
case 0:
return 9;
}
}
break;
case -1053468136:
if (var1.equals("getCallbacks")) {
switch(var2.length) {
case 0:
return 12;
}
}
break;
case -60403779:
if (var1.equals("CGLIB$SET_STATIC_CALLBACKS")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) {
return 13;
}
}
}
break;
case 85179481:
if (var1.equals("CGLIB$SET_THREAD_CALLBACKS")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) {
return 14;
}
}
}
break;
case 161998109:
if (var1.equals("CGLIB$STATICHOOK1")) {
switch(var2.length) {
case 0:
return 21;
}
}
break;
case 180909336:
if (var1.equals("CGLIB$doSomething$0")) {
switch(var2.length) {
case 0:
return 22;
}
}
break;
case 495524492:
if (var1.equals("setCallbacks")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) {
return 10;
}
}
}
break;
case 1154623345:
if (var1.equals("CGLIB$findMethodProxy")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("net.sf.cglib.core.Signature")) {
return 20;
}
}
}
break;
case 1794410543:
if (var1.equals("doSomething")) {
switch(var2.length) {
case 0:
return 8;
}
}
break;
case 1811874389:
if (var1.equals("newInstance")) {
switch(var2.length) {
case 1:
String var10001 = var2[0].getName();
switch(var10001.hashCode()) {
case -845341380:
if (var10001.equals("net.sf.cglib.proxy.Callback")) {
return 6;
}
break;
case 1730110032:
if (var10001.equals("[Lnet.sf.cglib.proxy.Callback;")) {
return 5;
}
}
case 2:
default:
break;
case 3:
if (var2[0].getName().equals("[Ljava.lang.Class;") && var2[1].getName().equals("[Ljava.lang.Object;") && var2[2].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) {
return 4;
}
}
}
break;
case 1817099975:
if (var1.equals("setCallback")) {
switch(var2.length) {
case 2:
if (var2[0].getName().equals("int") && var2[1].getName().equals("net.sf.cglib.proxy.Callback")) {
return 7;
}
}
}
break;
case 1905679803:
if (var1.equals("getCallback")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("int")) {
return 11;
}
}
}
break;
...
return -1;
}
public int getIndex(Class[] var1) {
switch(var1.length) {
case 0:
return 0;
default:
return -1;
}
}
// 根据index var1调用代理对象var2的方法
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
148ae4e var10000 = (148ae4e)var2;
int var10001 = var1;
try {
switch(var10001) {
...
case 4:
return var10000.newInstance((Class[])var3[0], (Object[])var3[1], (Callback[])var3[2]);
case 5:
return var10000.newInstance((Callback[])var3[0]);
case 6:
return var10000.newInstance((Callback)var3[0]);
case 7:
var10000.setCallback(((Number)var3[0]).intValue(), (Callback)var3[1]);
return null;
case 8:
// 调用代理对象的doSomething方法
var10000.doSomething();
return null;
case 9:
// 调用代理对象的doSomethingAgain方法
var10000.doSomethingAgain();
return null;
case 10:
var10000.setCallbacks((Callback[])var3[0]);
return null;
case 11:
return var10000.getCallback(((Number)var3[0]).intValue());
case 12:
return var10000.getCallbacks();
case 13:
148ae4e.CGLIB$SET_STATIC_CALLBACKS((Callback[])var3[0]);
return null;
case 14:
148ae4e.CGLIB$SET_THREAD_CALLBACKS((Callback[])var3[0]);
return null;
case 15:
var10000.CGLIB$doSomethingAgain$1();
return null;
...
case 20:
return 148ae4e.CGLIB$findMethodProxy((Signature)var3[0]);
case 21:
148ae4e.CGLIB$STATICHOOK1();
return null;
case 22:
var10000.CGLIB$doSomething$0();
return null;
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
148ae4e var10000 = new 148ae4e;
148ae4e var10001 = var10000;
int var10002 = var1;
try {
switch(var10002) {
case 0:
var10001.();
return var10000;
}
} catch (Throwable var3) {
throw new InvocationTargetException(var3);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 22;
}
}
fastClass机制是为了加快执行速度、减少运行时反射,通过上述代码分析,可以明确method.invoke和methodProxy.invokeSuper的区别
Spring的动态代理是怎么玩的
Spring Aop及周边框架大量用到动态代理,在Spring中通过注解使用Aop:
@Configuration
@Aspect
public class AopTest {
@Pointcut("execution(* com.example.demo.controller.*.*(..))")
public void pointCut() {
System.out.println("This is a pointcut");
}
@Before("pointCut()")
public void begin() {
System.out.println("Pointcut begin");
}
@After("pointCut()")
public void after() {
System.out.println("Pointcut end");
}
}
@Configuration
@Aspect
public class AopAnotherTest {
@Pointcut("execution(* com.example.demo.controller.*.*(..))")
public void pointAnotherCut() {
System.out.println("This is a another pointcut");
}
@Before("pointAnotherCut()")
public void begin() {
System.out.println("Another pointcut begin");
}
@After("pointAnotherCut()")
public void after() {
System.out.println("Another pointcut end");
}
}
@RestController
public class AopController implements Datasource {
@RequestMapping(path = "/aop/test", method = RequestMethod.GET)
public ResponseEntity testAop() {
System.out.println("testAop");
return ResponseEntity.ok().body(null);
}
}
除了上述代码外,还需要在启动类添加注解@EnableAspectJAutoProxy
之所以设置两个切面,主要是想说明Spring是对所有切面进行统一处理,而不是每个单独处理一次,执行结果如下:
Another pointcut begin
Pointcut begin
testAop
Pointcut end
Another pointcut end
Spring在bean初始化之前通过beanPostProcessor机制创建代理类,具体流程见图:
其中getAdvisors部分会将该bean所有方法相关的advisor组织到一起处理,如下:
// 将class的所有method过一遍
for (Method method : getAdvisorMethods(aspectClass)) {
// 获取method对应的Advisor
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
if (advisor != null) {
advisors.add(advisor);
}
}
JDK动态代理部分会获取到该bean类的所有接口,然后附加一些SpringProxy的特有接口,调用Proxy生成动态代理类,重点看下Cglib处理,和之前的讲述稍微有点不同
Cglib getProxy方法:
public Object getProxy(@Nullable ClassLoader classLoader) {
...
// 获取被代理类
Class> rootClass = this.advised.getTargetClass();
Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");
Class> proxySuperClass = rootClass;
// 如果该类已经是cglib代理类
if (ClassUtils.isCglibProxyClass(rootClass)) {
// 获取原被代理类,将要生成的动态代理类为该类的子类
proxySuperClass = rootClass.getSuperclass();
Class>[] additionalInterfaces = rootClass.getInterfaces();
for (Class> additionalInterface : additionalInterfaces) {
this.advised.addInterface(additionalInterface);
}
}
...
// Configure CGLIB Enhancer...
Enhancer enhancer = createEnhancer();
if (classLoader != null) {
enhancer.setClassLoader(classLoader);
if (classLoader instanceof SmartClassLoader &&
((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
enhancer.setUseCache(false);
}
}
// 生成的动态代理类为原被代理类的子类
enhancer.setSuperclass(proxySuperClass);
// 获取被代理类所有接口,附加SpringProxy特有接口
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader));
// 重点关注callback逻辑
Callback[] callbacks = getCallbacks(rootClass);
Class>[] types = new Class>[callbacks.length];
for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass();
}
// callbackFilter的作用是根据条件过滤出调用的callback
enhancer.setCallbackFilter(new ProxyCallbackFilter(
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
enhancer.setCallbackTypes(types);
// Generate the proxy class and create a proxy instance.
return createProxyClassAndInstance(enhancer, callbacks);
重点看下getCallBacks:
private Callback[] getCallbacks(Class> rootClass) throws Exception {
// 和EnableAspectJAutoProxy配置有关,是否将proxy暴露到上下文AopContext,默认为false
boolean exposeProxy = this.advised.isExposeProxy();
// 动态代理配置是否不允许修改,和优化有关,默认为false
boolean isFrozen = this.advised.isFrozen();
// 是否每次都返回同一个代理对象,也就是代理对象可以被缓存,跟bean的生命周期有关
boolean isStatic = this.advised.getTargetSource().isStatic();
// Choose an "aop" interceptor (used for AOP calls).
// 这个是重点,aop代理就走这个interceptor
Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised);
// Choose a "straight to target" interceptor. (used for calls that are
// unadvised but can return this). May be required to expose the proxy.
Callback targetInterceptor;
if (exposeProxy) {
targetInterceptor = isStatic ?
new StaticUnadvisedExposedInterceptor(this.advised.getTargetSource().getTarget()) :
new DynamicUnadvisedExposedInterceptor(this.advised.getTargetSource());
}
else {
targetInterceptor = isStatic ?
// 一般走这里
new StaticUnadvisedInterceptor(this.advised.getTargetSource().getTarget()) :
new DynamicUnadvisedInterceptor(this.advised.getTargetSource());
}
// Choose a "direct to target" dispatcher (used for
// unadvised calls to static targets that cannot return this).
Callback targetDispatcher = isStatic ?
new StaticDispatcher(this.advised.getTargetSource().getTarget()) : new SerializableNoOp();
Callback[] mainCallbacks = new Callback[] {
aopInterceptor, // for normal advice
targetInterceptor, // invoke target without considering advice, if optimized
new SerializableNoOp(), // no override for methods mapped to this
targetDispatcher, this.advisedDispatcher,
new EqualsInterceptor(this.advised),
new HashCodeInterceptor(this.advised)
};
...
}
看下Aop相关的DynamicAdvisedInterceptor的intercept方法:
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object oldProxy = null;
boolean setProxyContext = false;
Object target = null;
TargetSource targetSource = this.advised.getTargetSource();
try {
...
target = targetSource.getTarget();
Class> targetClass = (target != null ? target.getClass() : null);
// 重点:将所有的MethodInterceptor串成一个链
List
所有的methodInterceptor组成chain,依次调用,最后一个methodinterceptor调用methodProxy的invoke方法:
methodProxy.invoke(this.target, this.arguments);
这里的target为原始bean,不会陷入死循环,和cglib的分析呼应。