CGLIB动态代理原理

本文详细介绍了CGLIB动态代理的工作原理,包括代理类的生成、增强类的使用,以及方法调用的底层流程。通过DebuggingClassWriter设置动态生成的代理类存储位置,并分析了代理方法如何最终调用到原始类的方法。
摘要由CSDN通过智能技术生成

被代理类

public class Human {
    void doThings() {
        System.out.println("Human");
    }
}

代理类

public class CglibProxyHuman implements MethodInterceptor {

    public Object getProxyInstance(Object target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this); //将自己传给增强类,调用this.intercept
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before target method...");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("after target method...");
        return result;
    }
}

测试代码

 public static void main(String[] args) {
        //调试查看动态代理生成文件
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "f:\\cglib");
        Human human= new Human();
        Human humanProxy=(Human)new CglibProxyHuman().getProxyInstance(human);
        humanProxy.doThings();
    }

查看动态代理生成文件,存储在 f:\\cglib

System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "f:\\cglib");

增强类

humanProxy对象对应的动态生成的增强类 Human$ $EnhancerByCGLIB$$b7eedfef

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 Human$$EnhancerByCGLIB$$b7eedfef extends Human implements Factory {
    private boolean CGLIB$BOUND;
    private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
    private static final Callback[] CGLIB$STATIC_CALLBACKS;
    private MethodInterceptor CGLIB$CALLBACK_0;
    private static final Method CGLIB$doThings$0$Method;
    private static final MethodProxy CGLIB$doThings$0$Proxy;
    private static final Object[] CGLIB$emptyArgs;
    private static final Method CGLIB$finalize$1$Method;
    private static final MethodProxy CGLIB$finalize$1$Proxy;
    private static final Method CGLIB$equals$2$Method;
    private static final MethodProxy CGLIB$equals$2$Proxy;
    private static final Method CGLIB$toString$3$Method;
    private static final MethodProxy CGLIB$toString$3$Proxy;
    private static final Method CGLIB$hashCode$4$Method;
    private static final MethodProxy CGLIB$hashCode$4$Proxy;
    private static final Method CGLIB$clone$5$Method;
    private static final MethodProxy CGLIB$clone$5$Proxy;

    static void CGLIB$STATICHOOK1() {
        CGLIB$THREAD_CALLBACKS = new ThreadLocal();
        CGLIB$emptyArgs = new Object[0];
        Class 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值