日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用 cglib_CGLib 动态代理 原理解析

發布時間:2025/3/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 cglib_CGLib 动态代理 原理解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CGLib的基本使用

  使用一下CGLib,在JDK動態代理中提供一個Proxy類來創建代理類,而在CGLib動態代理中也提供了一個類似的類Enhancer;

  使用的CGLib版本是2.2.2,我是隨便找的,不同的版本有點小差異,建議用3.x版本的.....我用的maven項目進行測試的,首先要導入cglib的依賴

<dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2.2</version> </dependency>

目標類(一個公開方法,另外一個用final修飾):

package com.wyq.day527;public class Dog{final public void run(String name) {System.out.println("狗"+name+"----run");}public void eat() {System.out.println("狗----eat");} }

方法攔截器:

package com.wyq.day527;import java.lang.reflect.Method;import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy;public class MyMethodInterceptor implements MethodInterceptor{@Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {System.out.println("這里是對目標類進行增強!!!");//注意這里的方法調用,不是用反射哦!!!Object object = proxy.invokeSuper(obj, args);return object;} }

測試類:

package com.wyq.day527;import net.sf.cglib.core.DebuggingClassWriter; import net.sf.cglib.proxy.Enhancer;public class CgLibProxy {public static void main(String[] args) {//在指定目錄下生成動態代理類,我們可以反編譯看一下里面到底是一些什么東西System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:javajava_workapace");//創建Enhancer對象,類似于JDK動態代理的Proxy類,下一步就是設置幾個參數Enhancer enhancer = new Enhancer();//設置目標類的字節碼文件enhancer.setSuperclass(Dog.class);//設置回調函數enhancer.setCallback(new MyMethodInterceptor());//這里的creat方法就是正式創建代理類Dog proxyDog = (Dog)enhancer.create();//調用代理類的eat方法proxyDog.eat(); } }

測試結果

2.生成動態代理類

在這個代理類中對應會有eat 和CGLIB$eat$0這兩個方法;其中前者則是我們使用代理類時候調用的方法,后者是在方法攔截器里面調用的,換句話來說當我們代碼調用代理對象的eat方法,然后會到方法攔截器中調用intercept方法,該方法內則通過proxy.invokeSuper調用CGLIB$eat$0這個方法,

package defpackage;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;/* renamed from: Dog$$EnhancerByCGLIB$$8339ca18 reason: default package */ /* compiled from: <generated> */ public class Dog$$EnhancerByCGLIB$$8339ca18 extends Dog implements Factory {private static Object CGLIB$CALLBACK_FILTER;public static Object CGLIB$FACTORY_DATA;private static final Callback[] CGLIB$STATIC_CALLBACKS = null;private static final ThreadLocal CGLIB$THREAD_CALLBACKS = null;private static final Method CGLIB$clone$5$Method = null;private static final MethodProxy CGLIB$clone$5$Proxy = null;private static final Method CGLIB$eat$0$Method = null;private static final MethodProxy CGLIB$eat$0$Proxy = null;private static final Object[] CGLIB$emptyArgs = null;private static final Method CGLIB$equals$2$Method = null;private static final MethodProxy CGLIB$equals$2$Proxy = null;private static final Method CGLIB$hashCode$4$Method = null;private static final MethodProxy CGLIB$hashCode$4$Proxy = null;private static final Method CGLIB$runing$1$Method = null;private static final MethodProxy CGLIB$runing$1$Proxy = null;private static final Method CGLIB$toString$3$Method = null;private static final MethodProxy CGLIB$toString$3$Proxy = null;private boolean CGLIB$BOUND;private MethodInterceptor CGLIB$CALLBACK_0;static {CGLIB$STATICHOOK1();}public Dog$$EnhancerByCGLIB$$8339ca18() {CGLIB$BIND_CALLBACKS(this);}private static final void CGLIB$BIND_CALLBACKS(Object obj) {Dog$$EnhancerByCGLIB$$8339ca18 dog$$EnhancerByCGLIB$$8339ca18 = (Dog$$EnhancerByCGLIB$$8339ca18) obj;if (!dog$$EnhancerByCGLIB$$8339ca18.CGLIB$BOUND) {dog$$EnhancerByCGLIB$$8339ca18.CGLIB$BOUND = true;Object obj2 = CGLIB$THREAD_CALLBACKS.get();if (obj2 != null || (obj2 = CGLIB$STATIC_CALLBACKS) != null) {dog$$EnhancerByCGLIB$$8339ca18.CGLIB$CALLBACK_0 = ((Callback[]) obj2)[0];}}}public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] callbackArr) {CGLIB$STATIC_CALLBACKS = callbackArr;}public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] callbackArr) {CGLIB$THREAD_CALLBACKS.set(callbackArr);}static void CGLIB$STATICHOOK1() {CGLIB$THREAD_CALLBACKS = new ThreadLocal();CGLIB$emptyArgs = new Object[0];Class<?> cls = Class.forName("Dog$$EnhancerByCGLIB$$8339ca18");Class<?> cls2 = Class.forName("Dog");Method[] findMethods = ReflectUtils.findMethods(new String[]{"eat", "()V", "runing", "(Ljava/lang/String;)V"}, cls2.getDeclaredMethods());CGLIB$eat$0$Method = findMethods[0];CGLIB$eat$0$Proxy = MethodProxy.create(cls2, cls, "()V", "eat", "CGLIB$eat$0");CGLIB$runing$1$Method = findMethods[1];CGLIB$runing$1$Proxy = MethodProxy.create(cls2, cls, "(Ljava/lang/String;)V", "runing", "CGLIB$runing$1");Class<?> cls3 = Class.forName("java.lang.Object");Method[] findMethods2 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, cls3.getDeclaredMethods());CGLIB$equals$2$Method = findMethods2[0];CGLIB$equals$2$Proxy = MethodProxy.create(cls3, cls, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");CGLIB$toString$3$Method = findMethods2[1];CGLIB$toString$3$Proxy = MethodProxy.create(cls3, cls, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");CGLIB$hashCode$4$Method = findMethods2[2];CGLIB$hashCode$4$Proxy = MethodProxy.create(cls3, cls, "()I", "hashCode", "CGLIB$hashCode$4");CGLIB$clone$5$Method = findMethods2[3];CGLIB$clone$5$Proxy = MethodProxy.create(cls3, cls, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");}public static MethodProxy CGLIB$findMethodProxy(Signature signature) {String obj = signature.toString();switch (obj.hashCode()) {case -1310345955:if (obj.equals("eat()V")) {return CGLIB$eat$0$Proxy;}break;case -508378822:if (obj.equals("clone()Ljava/lang/Object;")) {return CGLIB$clone$5$Proxy;}break;case 868402408:if (obj.equals("runing(Ljava/lang/String;)V")) {return CGLIB$runing$1$Proxy;}break;case 1826985398:if (obj.equals("equals(Ljava/lang/Object;)Z")) {return CGLIB$equals$2$Proxy;}break;case 1913648695:if (obj.equals("toString()Ljava/lang/String;")) {return CGLIB$toString$3$Proxy;}break;case 1984935277:if (obj.equals("hashCode()I")) {return CGLIB$hashCode$4$Proxy;}break;}return null;}/* access modifiers changed from: package-private */public final Object CGLIB$clone$5() throws CloneNotSupportedException {return Dog$$EnhancerByCGLIB$$8339ca18.super.clone();}/* access modifiers changed from: package-private */public final void CGLIB$eat$0() {Dog$$EnhancerByCGLIB$$8339ca18.super.eat();}/* access modifiers changed from: package-private */public final boolean CGLIB$equals$2(Object obj) {return Dog$$EnhancerByCGLIB$$8339ca18.super.equals(obj);}/* access modifiers changed from: package-private */public final int CGLIB$hashCode$4() {return Dog$$EnhancerByCGLIB$$8339ca18.super.hashCode();}/* access modifiers changed from: package-private */public final void CGLIB$runing$1(String str) {Dog$$EnhancerByCGLIB$$8339ca18.super.runing(str);}/* access modifiers changed from: package-private */public final String CGLIB$toString$3() {return Dog$$EnhancerByCGLIB$$8339ca18.super.toString();}/* access modifiers changed from: protected */public final Object clone() throws CloneNotSupportedException {MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;if (methodInterceptor == null) {CGLIB$BIND_CALLBACKS(this);methodInterceptor = this.CGLIB$CALLBACK_0;}return methodInterceptor != null ? methodInterceptor.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy) : Dog$$EnhancerByCGLIB$$8339ca18.super.clone();}public final void eat() {MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;if (methodInterceptor == null) {CGLIB$BIND_CALLBACKS(this);methodInterceptor = this.CGLIB$CALLBACK_0;}if (methodInterceptor != null) {methodInterceptor.intercept(this, CGLIB$eat$0$Method, CGLIB$emptyArgs, CGLIB$eat$0$Proxy);} else {Dog$$EnhancerByCGLIB$$8339ca18.super.eat();}}public final boolean equals(Object obj) {MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;if (methodInterceptor == null) {CGLIB$BIND_CALLBACKS(this);methodInterceptor = this.CGLIB$CALLBACK_0;}if (methodInterceptor == null) {return Dog$$EnhancerByCGLIB$$8339ca18.super.equals(obj);}Object intercept = methodInterceptor.intercept(this, CGLIB$equals$2$Method, new Object[]{obj}, CGLIB$equals$2$Proxy);if (intercept == null) {return false;}return ((Boolean) intercept).booleanValue();}public Callback getCallback(int i) {CGLIB$BIND_CALLBACKS(this);switch (i) {case 0:return this.CGLIB$CALLBACK_0;default:return null;}}public Callback[] getCallbacks() {CGLIB$BIND_CALLBACKS(this);return new Callback[]{this.CGLIB$CALLBACK_0};}public final int hashCode() {MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;if (methodInterceptor == null) {CGLIB$BIND_CALLBACKS(this);methodInterceptor = this.CGLIB$CALLBACK_0;}if (methodInterceptor == null) {return Dog$$EnhancerByCGLIB$$8339ca18.super.hashCode();}Object intercept = methodInterceptor.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);if (intercept == null) {return 0;}return ((Number) intercept).intValue();}public Object newInstance(Callback callback) {CGLIB$SET_THREAD_CALLBACKS(new Callback[]{callback});Dog$$EnhancerByCGLIB$$8339ca18 dog$$EnhancerByCGLIB$$8339ca18 = new Dog$$EnhancerByCGLIB$$8339ca18();CGLIB$SET_THREAD_CALLBACKS(null);return dog$$EnhancerByCGLIB$$8339ca18;}public Object newInstance(Class[] clsArr, Object[] objArr, Callback[] callbackArr) {CGLIB$SET_THREAD_CALLBACKS(callbackArr);switch (clsArr.length) {case 0:Dog$$EnhancerByCGLIB$$8339ca18 dog$$EnhancerByCGLIB$$8339ca18 = new Dog$$EnhancerByCGLIB$$8339ca18();CGLIB$SET_THREAD_CALLBACKS(null);return dog$$EnhancerByCGLIB$$8339ca18;default:throw new IllegalArgumentException("Constructor not found");}}public Object newInstance(Callback[] callbackArr) {CGLIB$SET_THREAD_CALLBACKS(callbackArr);Dog$$EnhancerByCGLIB$$8339ca18 dog$$EnhancerByCGLIB$$8339ca18 = new Dog$$EnhancerByCGLIB$$8339ca18();CGLIB$SET_THREAD_CALLBACKS(null);return dog$$EnhancerByCGLIB$$8339ca18;}public final void runing(String str) {MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;if (methodInterceptor == null) {CGLIB$BIND_CALLBACKS(this);methodInterceptor = this.CGLIB$CALLBACK_0;}if (methodInterceptor != null) {methodInterceptor.intercept(this, CGLIB$runing$1$Method, new Object[]{str}, CGLIB$runing$1$Proxy);return;}Dog$$EnhancerByCGLIB$$8339ca18.super.runing(str);}public void setCallback(int i, Callback callback) {switch (i) {case 0:this.CGLIB$CALLBACK_0 = (MethodInterceptor) callback;return;default:return;}}public void setCallbacks(Callback[] callbackArr) {this.CGLIB$CALLBACK_0 = (MethodInterceptor) callbackArr[0];}public final String toString() {MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;if (methodInterceptor == null) {CGLIB$BIND_CALLBACKS(this);methodInterceptor = this.CGLIB$CALLBACK_0;}return methodInterceptor != null ? (String) methodInterceptor.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy) : Dog$$EnhancerByCGLIB$$8339ca18.super.toString();} }

總結

以上是生活随笔為你收集整理的使用 cglib_CGLib 动态代理 原理解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。