Java中的cglib代理详解
作者:.番茄炒蛋
工作原理
使用
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.3.0</version> </dependency>
对类和接口分别进行代理
DemoService
package com.fanqiechaodan.user.service; /** * @author fanqiechaodan * @Classname DemoService * @Description * @Date 2023/3/1 19:44 */ public class DemoService { public void test(){ System.out.println("==================>test"); } }
DemoInterface
package com.fanqiechaodan.user.service; /** * @author fanqiechaodan * @Classname DemoInterface * @Description * @Date 2023/3/1 19:56 */ public interface DemoInterface { void test(); }
Demo
package com.fanqiechaodan.user; import com.fanqiechaodan.user.service.DemoInterface; import com.fanqiechaodan.user.service.DemoService; import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; /** * @author fanqiechaodan * @Classname Main * @Description * @Date 2023/3/1 19:47 */ public class Demo { public static void main(String[] args) { final DemoService target = new DemoService(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(DemoService.class); // 必须设置callbacks不然会出现java.lang.NullPointerException enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> { // test方法增强逻辑 if ("test".equals(method.getName())) { System.out.println("方法执行前增强..."); Object res = method.invoke(target, objects); System.out.println("方法执行后增强..."); return res; } // 其他方法正常执行 return method.invoke(target, objects); }}); DemoService demoService = (DemoService) enhancer.create(); demoService.test(); Enhancer enhancer1 = new Enhancer(); enhancer1.setSuperclass(DemoInterface.class); enhancer1.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> { // test方法增强逻辑 if ("test".equals(method.getName())) { System.out.println("切面逻辑..."); } return null; }}); DemoInterface demo = (DemoInterface) enhancer1.create(); demo.test(); } }
代码执行前设置VM options:
-Dcglib.debugLocation=根据自己实际情况填写
运行代码是cglib就会将生成的代理类放到上面所指定的路径上.
DemoService代理类
public class DemoService$$EnhancerByCGLIB$$1c9ab053 extends DemoService implements Factory { // 省去不重要代码... final void CGLIB$test$0() { super.test(); } public final void test() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { var10000.intercept(this, CGLIB$test$0$Method, CGLIB$emptyArgs, CGLIB$test$0$Proxy); } else { super.test(); } } // 省去不重要代码... }
DemoInterface代理类
public class DemoInterface$$EnhancerByCGLIB$$536bfe99 implements DemoInterface, Factory { // 省去不重要代码... final void CGLIB$test$4() { super.test(); } public final void test() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { var10000.intercept(this, CGLIB$test$4$Method, CGLIB$emptyArgs, CGLIB$test$4$Proxy); } else { super.test(); } } // 省去不重要代码... }
final void CGLIB$test$0() { super.test(); }
这个方法我们并不能直接调用,而是要通过所设置的Callback,也就是MethodInterceptor中的methodProxy来调用,methodProxy表示方法代理,假如DemoService代理对象在执行test()方法,那么当执行流程进入到intercept方法时,methodProxy表示的就是test()方法,DemoService和DemoService代理类中都有test(),所以methodProxy代理的就是这两个test()
工作原理:cglib会根据所设置得superclass,生成代理类作为其子类,并且会重写superclass中需要代理得方法,比如test(),相应得再代理类中会有两个方法,一个是重写的test(),用来执行增强逻辑,一个是CGLIB$test$0(),会直接调用super.test(),是让MethodProxy对象来用的
代理类生成逻辑
- DemoService代理类全部代码
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.fanqiechaodan.user.service; 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 DemoService$$EnhancerByCGLIB$$1c9ab053 extends DemoService 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$test$0$Method; private static final MethodProxy CGLIB$test$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.fanqiechaodan.user.service.DemoService$$EnhancerByCGLIB$$1c9ab053"); 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$test$0$Method = ReflectUtils.findMethods(new String[]{"test", "()V"}, (var1 = Class.forName("com.fanqiechaodan.user.service.DemoService")).getDeclaredMethods())[0]; CGLIB$test$0$Proxy = MethodProxy.create(var1, var0, "()V", "test", "CGLIB$test$0"); } final void CGLIB$test$0() { super.test(); } public final void test() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { var10000.intercept(this, CGLIB$test$0$Method, CGLIB$emptyArgs, CGLIB$test$0$Proxy); } else { super.test(); } } 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 -1422510685: if (var10000.equals("test()V")) { return CGLIB$test$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 DemoService$$EnhancerByCGLIB$$1c9ab053() { CGLIB$BIND_CALLBACKS(this); } // 将代码中设置的callbacks设置到CGLIB$THREAD_CALLBACKS这个ThreadLocal中 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) { DemoService$$EnhancerByCGLIB$$1c9ab053 var1 = (DemoService$$EnhancerByCGLIB$$1c9ab053)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); DemoService$$EnhancerByCGLIB$$1c9ab053 var10000 = new DemoService$$EnhancerByCGLIB$$1c9ab053(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; } public Object newInstance(Callback var1) { CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1}); DemoService$$EnhancerByCGLIB$$1c9ab053 var10000 = new DemoService$$EnhancerByCGLIB$$1c9ab053(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; } public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) { CGLIB$SET_THREAD_CALLBACKS(var3); DemoService$$EnhancerByCGLIB$$1c9ab053 var10000 = new DemoService$$EnhancerByCGLIB$$1c9ab053; 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(); } }
代理类中有一个静态代码块,会调用CGLIB$STATICHOOK1(),这个方法主要是为了给属性赋值,例如:
- 构造一个ThreadLocal对象赋值给CGLIB$THREAD_CALLBACKS
- 获取DemoService类中的test方法的Method对象赋值给CGLIB$test0 00Method
- 构造test()方法所对应的MethodProxy对象赋值给CGLIB$test0 00Proxy
DemoService的代理类即继承了DemoService,也实现了Factory接口,从而就需要实现Factory接口中的方法
public interface Factory { Object newInstance(Callback callback); Object newInstance(Callback[] callbacks); Object newInstance(Class[] types, Object[] args, Callback[] callbacks); Callback getCallback(int index); void setCallback(int index, Callback callback); void setCallbacks(Callback[] callbacks); Callback[] getCallbacks(); }
newInstance方法会重新生成一个代理对象,setCallbacks()和getCallbacks()用来设置或获取增强逻辑
代理类大致流程是:
- cglib先生成代理类,具体逻辑为:
- 先生成类的定义,继承DemoService和实现Factory
- 根据DemoService类中的方法生成代理类中对应的方法和属性
- 生成一些辅助的属性和方法
- 然后调用构造方法得到代理对象
- 然后cglib调用代理对象的CGLIB$SET_THREAD_CALLBACKS()方法,将我们自己所设置的callbacks设置到CGLIB$THREAD_CALLBACKS这个ThreadLocal中
- 后续代理对象再执行test()方法时,就会从CGLIB$THREAD_CALLBACKS拿到所设置的callbacks,调用其intercept()方法
到此这篇关于Java中的cglib代理详解的文章就介绍到这了,更多相关Java的cglib代理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!