CGLIB 通过动态生成一个需要被代理类的子类(即被代理类作为父类),该子类重写被代理类的所有不是 final 修饰的方法,并在子类中采用方法拦截的技术拦截父类所有的方法调用,进而织入横切逻辑。此外,因为 CGLIB 采用整型变量建立了方法索引,这比使用 JDK 动态代理更快(使用 Java 反射技术创建代理类的实例)。
1、需要被代理的类
package com.example.cglib.mapper.impl;
public class CglibProxyUserMapperImpl {
public void print() {
System.out.println("com.example.mapper.impl.CglibProxyUserMapperImpl.print ---> ");
}
}
/*
注意:
要被CGLIB动态代理的类不需要 加注解 或 实现接口
*/
2、MethodInterceptor实现类
package com.example.cglib.interceptors;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("com.example.interceptors.MyMethodInterceptor.intercept");
return methodProxy.invokeSuper(o, objects);
}
}
/*
MethodInterceptor 是 org.springframework.cglib.proxy 包下的
1)o:表示增强的对象,即实现这个接口类的一个对象;
2)method:表示要被拦截的方法;
3)objects:表示要被拦截方法的参数;
4)methodProxy:表示要触发父类的方法对象。
*/
3、测试类
package com.example.cglib.test;
import com.example.cglib.interceptors.MyMethodInterceptor;
import com.example.cglib.mapper.impl.CglibProxyUserMapperImpl;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
public class Test {
public static void main(String[] args) {
// 代理类class文件存入本地磁盘方便我们反编译查看源码
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:/_temp/cglibCode");
// 通过CGLIB动态代理获取代理对象的过程
Enhancer enhancer = new Enhancer();
// 设置enhancer对象的父类
enhancer.setSuperclass(CglibProxyUserMapperImpl.class);
// 设置enhancer对象的回调对象
enhancer.setCallback(new MyMethodInterceptor());
// 创建代理对象
CglibProxyUserMapperImpl cglibProxyUserMapper = (CglibProxyUserMapperImpl) enhancer.create();
// 通过代理对象调用目标方法
cglibProxyUserMapper.print();
}
}
4、控制台输出
CGLIB debugging enabled, writing to 'E:/_temp/cglibCode'
com.example.interceptors.MyMethodInterceptor.intercept
com.example.mapper.impl.CglibProxyUserMapperImpl.print --->
5、生成的代理类(扩展)
当前代码为 org.springframework.cglib.proxy.MethodProxy#invokeSuper 中的代码
// 某个方法
public Object invokeSuper(Object obj, Object[] args) throws Throwable {
try {
init();
FastClassInfo fci = fastClassInfo;
return fci.f2.invoke(fci.i2, obj, args);
}
catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
可断点调试 MethodProxy 类中的 fci.f2.invoke(fci.i2, obj, args);
然后观察 fci.i2 的取值,通过该取值去下方的 CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db$$FastClassByCGLIB$$cac95a8 类
中寻找对应的 case 分支。
若取值19(可在当前页面搜索"19:"),则调用 var10000.CGLIB$print$0(); 而 CGLIB$print$0() 该方法也可在下方的
CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db 中找到,即会调用 super.print(); 也就是被代理类的 print() 方法
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.example.cglib.mapper.impl;
import java.lang.reflect.Method;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
public class CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db extends CglibProxyUserMapperImpl 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$equals$1$Method;
private static final MethodProxy CGLIB$equals$1$Proxy;
private static final Method CGLIB$toString$2$Method;
private static final MethodProxy CGLIB$toString$2$Proxy;
private static final Method CGLIB$hashCode$3$Method;
private static final MethodProxy CGLIB$hashCode$3$Proxy;
private static final Method CGLIB$clone$4$Method;
private static final MethodProxy CGLIB$clone$4$Proxy;
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.example.cglib.mapper.impl.CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db");
Class var1;
Method[] 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$1$Method = var10000[0];
CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
CGLIB$toString$2$Method = var10000[1];
CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
CGLIB$hashCode$3$Method = var10000[2];
CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
CGLIB$clone$4$Method = var10000[3];
CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
CGLIB$print$0$Method = ReflectUtils.findMethods(new String[]{"print", "()V"}, (var1 = Class.forName("com.example.cglib.mapper.impl.CglibProxyUserMapperImpl")).getDeclaredMethods())[0];
CGLIB$print$0$Proxy = MethodProxy.create(var1, var0, "()V", "print", "CGLIB$print$0");
}
final void CGLIB$print$0() {
super.print();
}
public final void print() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
// 若 var10000 不为空,则先调用 MethodInterceptor 方法拦截器
if (var10000 != null) {
var10000.intercept(this, CGLIB$print$0$Method, CGLIB$emptyArgs, CGLIB$print$0$Proxy);
} else {
super.print();
}
}
final boolean CGLIB$equals$1(Object var1) {
return super.equals(var1);
}
public final boolean equals(Object var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);
return var2 == null ? false : (Boolean)var2;
} else {
return super.equals(var1);
}
}
final String CGLIB$toString$2() {
return super.toString();
}
public final String toString() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();
}
final int CGLIB$hashCode$3() {
return super.hashCode();
}
public final int hashCode() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
return var1 == null ? 0 : ((Number)var1).intValue();
} else {
return super.hashCode();
}
}
final Object CGLIB$clone$4() throws CloneNotSupportedException {
return super.clone();
}
protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch (var10000.hashCode()) {
case -1166389848:
if (var10000.equals("print()V")) {
return CGLIB$print$0$Proxy;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$4$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$1$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$2$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$3$Proxy;
}
}
return null;
}
public CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db() {
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;
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db var1 = (CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (var10000 == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db var10000 = new CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db var10000 = new CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db var10000 = new CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db;
switch (var1.length) {
case 0:
var10000.<init>();
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];
}
static {
CGLIB$STATICHOOK1();
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.example.cglib.mapper.impl;
import com.example.cglib.mapper.impl.CglibProxyUserMapperImpl..EnhancerByCGLIB..2a1a85db;
import java.lang.reflect.InvocationTargetException;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.reflect.FastClass;
public class CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db$$FastClassByCGLIB$$cac95a8 extends FastClass {
public CglibProxyUserMapperImpl$$EnhancerByCGLIB$$2a1a85db$$FastClassByCGLIB$$cac95a8(Class var1) {
super(var1);
}
public int getIndex(Signature var1) {
String var10000 = var1.toString();
switch (var10000.hashCode()) {
case -1882565338:
if (var10000.equals("CGLIB$equals$1(Ljava/lang/Object;)Z")) {
return 15;
}
break;
case -1870561232:
if (var10000.equals("CGLIB$findMethodProxy(Lorg/springframework/cglib/core/Signature;)Lorg/springframework/cglib/proxy/MethodProxy;")) {
return 20;
}
break;
case -1745842178:
if (var10000.equals("setCallbacks([Lorg/springframework/cglib/proxy/Callback;)V")) {
return 13;
}
break;
case -1641413109:
if (var10000.equals("newInstance([Lorg/springframework/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 6;
}
break;
case -1457535688:
if (var10000.equals("CGLIB$STATICHOOK1()V")) {
return 14;
}
break;
case -1411842725:
if (var10000.equals("CGLIB$hashCode$3()I")) {
return 18;
}
break;
case -1166389848:
if (var10000.equals("print()V")) {
return 7;
}
break;
case -1034266769:
if (var10000.equals("CGLIB$SET_STATIC_CALLBACKS([Lorg/springframework/cglib/proxy/Callback;)V")) {
return 12;
}
break;
case -1025895669:
if (var10000.equals("CGLIB$SET_THREAD_CALLBACKS([Lorg/springframework/cglib/proxy/Callback;)V")) {
return 11;
}
break;
case -988317324:
if (var10000.equals("newInstance([Ljava/lang/Class;[Ljava/lang/Object;[Lorg/springframework/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 4;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return 3;
}
break;
case -149311009:
if (var10000.equals("CGLIB$print$0()V")) {
return 19;
}
break;
case 610042816:
if (var10000.equals("newInstance(Lorg/springframework/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 5;
}
break;
case 1132856532:
if (var10000.equals("getCallbacks()[Lorg/springframework/cglib/proxy/Callback;")) {
return 9;
}
break;
case 1246779367:
if (var10000.equals("setCallback(ILorg/springframework/cglib/proxy/Callback;)V")) {
return 8;
}
break;
case 1306468936:
if (var10000.equals("CGLIB$toString$2()Ljava/lang/String;")) {
return 16;
}
break;
case 1364367423:
if (var10000.equals("getCallback(I)Lorg/springframework/cglib/proxy/Callback;")) {
return 10;
}
break;
case 1800494055:
if (var10000.equals("CGLIB$clone$4()Ljava/lang/Object;")) {
return 17;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return 0;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return 1;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return 2;
}
}
return -1;
}
public int getIndex(String var1, Class[] var2) {
switch (var1.hashCode()) {
case -1776922004:
if (var1.equals("toString")) {
switch (var2.length) {
case 0:
return 1;
}
}
break;
case -1295482945:
if (var1.equals("equals")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("java.lang.Object")) {
return 0;
}
}
}
break;
case -1053468136:
if (var1.equals("getCallbacks")) {
switch (var2.length) {
case 0:
return 9;
}
}
break;
case -124978609:
if (var1.equals("CGLIB$equals$1")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("java.lang.Object")) {
return 15;
}
}
}
break;
case -60403779:
if (var1.equals("CGLIB$SET_STATIC_CALLBACKS")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("[Lorg.springframework.cglib.proxy.Callback;")) {
return 12;
}
}
}
break;
case -29025555:
if (var1.equals("CGLIB$hashCode$3")) {
switch (var2.length) {
case 0:
return 18;
}
}
break;
case 85179481:
if (var1.equals("CGLIB$SET_THREAD_CALLBACKS")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("[Lorg.springframework.cglib.proxy.Callback;")) {
return 11;
}
}
}
break;
case 94756189:
if (var1.equals("clone")) {
switch (var2.length) {
case 0:
return 3;
}
}
break;
case 106934957:
if (var1.equals("print")) {
switch (var2.length) {
case 0:
return 7;
}
}
break;
case 147696667:
if (var1.equals("hashCode")) {
switch (var2.length) {
case 0:
return 2;
}
}
break;
case 161998109:
if (var1.equals("CGLIB$STATICHOOK1")) {
switch (var2.length) {
case 0:
return 14;
}
}
break;
case 495524492:
if (var1.equals("setCallbacks")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("[Lorg.springframework.cglib.proxy.Callback;")) {
return 13;
}
}
}
break;
case 770871766:
if (var1.equals("CGLIB$print$0")) {
switch (var2.length) {
case 0:
return 19;
}
}
break;
case 1154623345:
if (var1.equals("CGLIB$findMethodProxy")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("org.springframework.cglib.core.Signature")) {
return 20;
}
}
}
break;
case 1543336189:
if (var1.equals("CGLIB$toString$2")) {
switch (var2.length) {
case 0:
return 16;
}
}
break;
case 1811874389:
if (var1.equals("newInstance")) {
switch (var2.length) {
case 1:
String var10001 = var2[0].getName();
switch (var10001.hashCode()) {
case -1997738671:
if (var10001.equals("[Lorg.springframework.cglib.proxy.Callback;")) {
return 6;
}
break;
case 1364160985:
if (var10001.equals("org.springframework.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("[Lorg.springframework.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("org.springframework.cglib.proxy.Callback")) {
return 8;
}
}
}
break;
case 1905679803:
if (var1.equals("getCallback")) {
switch (var2.length) {
case 1:
if (var2[0].getName().equals("int")) {
return 10;
}
}
}
break;
case 1951977610:
if (var1.equals("CGLIB$clone$4")) {
switch (var2.length) {
case 0:
return 17;
}
}
}
return -1;
}
public int getIndex(Class[] var1) {
switch (var1.length) {
case 0:
return 0;
default:
return -1;
}
}
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
CglibProxyUserMapperImpl..EnhancerByCGLIB..2a1a85db var10000 = (CglibProxyUserMapperImpl..EnhancerByCGLIB..2a1a85db)var2;
int var10001 = var1;
try {
switch (var10001) {
case 0:
return new Boolean(var10000.equals(var3[0]));
case 1:
return var10000.toString();
case 2:
return new Integer(var10000.hashCode());
case 3:
return var10000.clone();
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.print();
return null;
case 8:
var10000.setCallback(((Number)var3[0]).intValue(), (Callback)var3[1]);
return null;
case 9:
return var10000.getCallbacks();
case 10:
return var10000.getCallback(((Number)var3[0]).intValue());
case 11:
2a1a85db.CGLIB$SET_THREAD_CALLBACKS((Callback[])var3[0]);
return null;
case 12:
2a1a85db.CGLIB$SET_STATIC_CALLBACKS((Callback[])var3[0]);
return null;
case 13:
var10000.setCallbacks((Callback[])var3[0]);
return null;
case 14:
2a1a85db.CGLIB$STATICHOOK1();
return null;
case 15:
return new Boolean(var10000.CGLIB$equals$1(var3[0]));
case 16:
return var10000.CGLIB$toString$2();
case 17:
return var10000.CGLIB$clone$4();
case 18:
return new Integer(var10000.CGLIB$hashCode$3());
case 19:
var10000.CGLIB$print$0();
return null;
case 20:
return 2a1a85db.CGLIB$findMethodProxy((Signature)var3[0]);
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
CglibProxyUserMapperImpl..EnhancerByCGLIB..2a1a85db var10000 = new CglibProxyUserMapperImpl..EnhancerByCGLIB..2a1a85db;
CglibProxyUserMapperImpl..EnhancerByCGLIB..2a1a85db var10001 = var10000;
int var10002 = var1;
try {
switch (var10002) {
case 0:
var10001.<init>();
return var10000;
}
} catch (Throwable var3) {
throw new InvocationTargetException(var3);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 20;
}
}