最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中的cglib代理詳解

 更新時(shí)間:2023年09月20日 09:36:20   作者:.番茄炒蛋  
這篇文章主要介紹了Java中的cglib代理詳解, 代理模式是一種設(shè)計(jì)模式,它可以為其他對(duì)象提供一種代理,以控制對(duì)該對(duì)象的訪問(wèn),可以在運(yùn)行時(shí)動(dòng)態(tài)地創(chuàng)建代理對(duì)象,而不需要手動(dòng)編寫代理類的代碼,需要的朋友可以參考下

工作原理

使用

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

對(duì)類和接口分別進(jìn)行代理

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);
        // 必須設(shè)置callbacks不然會(huì)出現(xiàn)java.lang.NullPointerException
        enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {
            // test方法增強(qiáng)邏輯
            if ("test".equals(method.getName())) {
                System.out.println("方法執(zhí)行前增強(qiáng)...");
                Object res = method.invoke(target, objects);
                System.out.println("方法執(zhí)行后增強(qiáng)...");
                return res;
            }
            // 其他方法正常執(zhí)行
            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方法增強(qiáng)邏輯
            if ("test".equals(method.getName())) {
                System.out.println("切面邏輯...");
            }
            return null;
        }});
        DemoInterface demo = (DemoInterface) enhancer1.create();
        demo.test();
    }
}

代碼執(zhí)行前設(shè)置VM options:

-Dcglib.debugLocation=根據(jù)自己實(shí)際情況填寫

運(yùn)行代碼是cglib就會(huì)將生成的代理類放到上面所指定的路徑上.

在這里插入圖片描述

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();
    }

這個(gè)方法我們并不能直接調(diào)用,而是要通過(guò)所設(shè)置的Callback,也就是MethodInterceptor中的methodProxy來(lái)調(diào)用,methodProxy表示方法代理,假如DemoService代理對(duì)象在執(zhí)行test()方法,那么當(dāng)執(zhí)行流程進(jìn)入到intercept方法時(shí),methodProxy表示的就是test()方法,DemoService和DemoService代理類中都有test(),所以methodProxy代理的就是這兩個(gè)test()

在這里插入圖片描述

工作原理:cglib會(huì)根據(jù)所設(shè)置得superclass,生成代理類作為其子類,并且會(huì)重寫superclass中需要代理得方法,比如test(),相應(yīng)得再代理類中會(huì)有兩個(gè)方法,一個(gè)是重寫的test(),用來(lái)執(zhí)行增強(qiáng)邏輯,一個(gè)是CGLIB$test$0(),會(huì)直接調(diào)用super.test(),是讓MethodProxy對(duì)象來(lái)用的

代理類生成邏輯

  • 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);
    }
     // 將代碼中設(shè)置的callbacks設(shè)置到CGLIB$THREAD_CALLBACKS這個(gè)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();
    }
}

代理類中有一個(gè)靜態(tài)代碼塊,會(huì)調(diào)用CGLIB$STATICHOOK1(),這個(gè)方法主要是為了給屬性賦值,例如:

  1. 構(gòu)造一個(gè)ThreadLocal對(duì)象賦值給CGLIB$THREAD_CALLBACKS
  2. 獲取DemoService類中的test方法的Method對(duì)象賦值給CGLIB$test0 00Method
  3. 構(gòu)造test()方法所對(duì)應(yīng)的MethodProxy對(duì)象賦值給CGLIB$test0 00Proxy

DemoService的代理類即繼承了DemoService,也實(shí)現(xiàn)了Factory接口,從而就需要實(shí)現(xiàn)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方法會(huì)重新生成一個(gè)代理對(duì)象,setCallbacks()和getCallbacks()用來(lái)設(shè)置或獲取增強(qiáng)邏輯

代理類大致流程是:

  1. cglib先生成代理類,具體邏輯為:
    • 先生成類的定義,繼承DemoService和實(shí)現(xiàn)Factory
    • 根據(jù)DemoService類中的方法生成代理類中對(duì)應(yīng)的方法和屬性
    • 生成一些輔助的屬性和方法
  2. 然后調(diào)用構(gòu)造方法得到代理對(duì)象
  3. 然后cglib調(diào)用代理對(duì)象的CGLIB$SET_THREAD_CALLBACKS()方法,將我們自己所設(shè)置的callbacks設(shè)置到CGLIB$THREAD_CALLBACKS這個(gè)ThreadLocal中
  4. 后續(xù)代理對(duì)象再執(zhí)行test()方法時(shí),就會(huì)從CGLIB$THREAD_CALLBACKS拿到所設(shè)置的callbacks,調(diào)用其intercept()方法

到此這篇關(guān)于Java中的cglib代理詳解的文章就介紹到這了,更多相關(guān)Java的cglib代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 命名規(guī)范(非常全面)

    Java 命名規(guī)范(非常全面)

    在本文中,將從大到小,從外到內(nèi),總結(jié)Java編程中的命名規(guī)范。文中將會(huì)涉及到日常工作中常見的命名示例,如包命名,類命名,接口命名,方法命名,變量命名,常類命名,抽象類命名,異常類命名以及擴(kuò)展類命名等。
    2021-09-09
  • Java編寫簡(jiǎn)易rabbitmq生產(chǎn)者與消費(fèi)者的代碼

    Java編寫簡(jiǎn)易rabbitmq生產(chǎn)者與消費(fèi)者的代碼

    開發(fā)時(shí)經(jīng)常與其它系統(tǒng)用rabbitmq對(duì)接,當(dāng)需要自測(cè)時(shí),還是自己寫rabbitmq生產(chǎn)者、消費(fèi)者自測(cè)方便些,下面給大家總結(jié)使用java編寫簡(jiǎn)易rabbitmq的方法,感興趣的朋友一起看看吧
    2023-11-11
  • application.yaml與bootstrap.yaml的使用

    application.yaml與bootstrap.yaml的使用

    這篇文章主要介紹了application.yaml與bootstrap.yaml的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題

    Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題

    這篇文章主要介紹了Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題,需要的朋友可以參考下
    2017-06-06
  • SpringBoot項(xiàng)目啟動(dòng)失敗:APPLICATION FAILED TO START的解決方法

    SpringBoot項(xiàng)目啟動(dòng)失?。篈PPLICATION FAILED TO START的

    文章介紹了在使用Spring Boot 4.0.0版本時(shí)遇到的Mapper文件找不到的問(wèn)題,并通過(guò)將依賴版本切換到3.5.7來(lái)解決問(wèn)題,此外,文章還提供了關(guān)于Spring Boot新版本使用注意事項(xiàng)的總結(jié),包括版本兼容性、配置變更、新特性、遷移建議和常見問(wèn)題,需要的朋友可以參考下
    2025-11-11
  • JRebel2023.3 插件使用及安裝步驟詳解

    JRebel2023.3 插件使用及安裝步驟詳解

    JRebel是一款JVM插件,它使得Java代碼修改后不用重啟系統(tǒng),立即生效,IDEA上原生是不支持熱部署的,一般更新了 Java 文件后要手動(dòng)重啟 Tomcat 服務(wù)器,才能生效,浪費(fèi)時(shí)間浪費(fèi)生命,目前對(duì)于idea熱部署最好的解決方案就是安裝JRebel插件,本文分步驟介紹的非常詳細(xì),一起看看吧
    2023-08-08
  • Java JNA庫(kù)詳解與本地系統(tǒng)交互實(shí)戰(zhàn)記錄(推薦)

    Java JNA庫(kù)詳解與本地系統(tǒng)交互實(shí)戰(zhàn)記錄(推薦)

    JNA 是一個(gè)開源庫(kù),它提供了 Java 程序調(diào)用本地共享庫(kù)(如 DLLs 在 Windows 或 .so 文件在 Linux/Unix)的功能,本文給大家介紹Java JNA庫(kù)詳解與本地系統(tǒng)交互實(shí)戰(zhàn),感興趣的朋友一起看看吧
    2025-11-11
  • 關(guān)于SpringBoot+Mybatis報(bào)MapperScan.factoryBean()問(wèn)題

    關(guān)于SpringBoot+Mybatis報(bào)MapperScan.factoryBean()問(wèn)題

    解決SpringBoot+Mybatis中的MapperScan.factoryBean()問(wèn)題,讓你的項(xiàng)目運(yùn)行更順暢!本指南將帶你一步步解決這個(gè)問(wèn)題,讓你的開發(fā)過(guò)程更加高效,不要錯(cuò)過(guò)這個(gè)實(shí)用指南,快來(lái)一探究竟吧!
    2024-02-02
  • Spring中的@Value和@PropertySource注解詳解

    Spring中的@Value和@PropertySource注解詳解

    這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中,本文提供了部分實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-11-11
  • 一文了解jJava中的加密與安全

    一文了解jJava中的加密與安全

    常見的編碼有ASCII碼、Unicode編碼。最簡(jiǎn)單的編碼是直接給每個(gè)字符指定一個(gè)若干字節(jié)表示的整數(shù),復(fù)雜一點(diǎn)的編碼就需要根據(jù)已有的編碼推算出來(lái)。本文將為大家詳細(xì)講講Java重點(diǎn)加密與安全,感興趣的可以了解一下
    2022-07-07

最新評(píng)論

濮阳市| 乐昌市| 锡林郭勒盟| 郓城县| 特克斯县| 县级市| 公安县| 广水市| 宜城市| 周口市| 蒙阴县| 灵丘县| 岳普湖县| 苏州市| 高邮市| 渭源县| 醴陵市| 克山县| 体育| 若羌县| 合江县| 忻城县| 大宁县| 文昌市| 海原县| 竹山县| 中宁县| 香河县| 秭归县| 湟中县| 松溪县| 德格县| 嵩明县| 凉城县| 依安县| 弋阳县| 靖远县| 句容市| 涡阳县| 宾阳县| 尉氏县|