zoukankan      html  css  js  c++  java
  • cglib调用过程

    本篇文章不讲cglib如何应用,只从eclipse调试的角度看看从最外层的动态代理类调用到最内层的被代理类,经过了哪些中间步骤。

    废话不多说,直接上代码

    被代理类:

    public class MySubject {
        public void doSomeThing() {
            System.err.println("Do some thing!");
        }    
    }

    拦截器:

    public class CgLibMethodInterceptor implements MethodInterceptor {
    	@Override
    	public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    		System.err.println("before ");
    		Object result = proxy.invokeSuper(target, args);
    		System.err.println("after " + result);
    		return result;
    	}
    
    } 

    测试cglib动态代理:

    public class CglibProxyWriter {
    	public static void main(String[] args) {
    		System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\cglib-class\");
    		MySubject subject= (MySubject) Enhancer.create(MySubject.class,new CgLibMethodInterceptor());		
    		subject.doSomeThing();
    	}
    }
    

     

    从调用栈可以清晰的看到最外层到最内层的调用逻辑。

    MySubject$$EnhancerByCGLIB$$24226c3c(MySubject).doSomeThing() line: 6
    MySubject$$EnhancerByCGLIB$$24226c3c.CGLIB$doSomeThing$0() line: not available
    MySubject$$EnhancerByCGLIB$$24226c3c$$FastClassByCGLIB$$a7941ac2.invoke(int, Object, Object[]) line: not available
    MethodProxy.invokeSuper(Object, Object[]) line: 244
    CgLibMethodInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 14
    MySubject$$EnhancerByCGLIB$$24226c3c.doSomeThing() line: not available
    CglibProxyWriter.main(String[]) line: 16

    MySubject$$EnhancerByCGLIB$$24226c3c(MySubject).doSomeThing() line: 6
    
    

     MySubject$$EnhancerByCGLIB$$24226c3c.CGLIB$doSomeThing$0() line: not available 

     

    MySubject$$EnhancerByCGLIB$$24226c3c$$FastClassByCGLIB$$a7941ac2.invoke(int, Object, Object[]) line: not available
    
    

     MethodProxy.invokeSuper(Object, Object[]) line: 244

     CgLibMethodInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 14

    MySubject$$EnhancerByCGLIB$$24226c3c.doSomeThing() line: not available

     CglibProxyWriter.main(String[]) line: 16

     

      

    eclipse调试的时候不能加上断点,cglib动态生成的代理类需要反编译工具查看,截图已经提供了反编译后的代码。

     

     

  • 相关阅读:
    飞思卡尔IMX6处理器的GPIO配置方式
    批处理清除VisualStudio解决方案文件夹
    总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
    详解Linux2.6内核中基于platform机制的驱动模型 (经典)
    [驱动注册]platform_driver_register()与platform_device_register()
    机器人系统常用仿真软件介绍效果与评价指标
    WINCE的批处理
    项目开发中的人月及如何计算
    常用的六个富文本编辑器
    如何获取公众号里面的歌曲
  • 原文地址:https://www.cnblogs.com/pmh905001/p/11456150.html
Copyright © 2011-2022 走看看