博客
关于我
动态代理 (CGLIB 动态代理)
阅读量:564 次
发布时间:2019-03-09

本文共 1973 字,大约阅读时间需要 6 分钟。

动态代理与CGLib实现

在软件开发中,动态代理是一种常用的技术,它允许在程序运行时为目标对象创建代理类,以实现对目标对象的控制和扩展。其中,CGLib动态代理是一种基于字节码技术的动态代理实现方式,提供了高度的灵活性和对方法拦截的强大能力。

CGLib动态代理的原理

CGLib动态代理通过生成目标类的子类,拦截并重写目标类的所有方法。在生成的子类中,通过 JNI(本地方法接口)实现对目标方法的动态调用,并在调用过程中可以插入自定义的逻辑。这种方式能够在不修改目标类的前提下,动态地为目标类添加功能或改变行为。

CGLib动态代理的实现步骤

1. 定义被代理类

定义一个普通的 Java 类作为目标类:

package com.cglibdynamicproxy.service;public class AgentTarget {    public void proxyMethod() {        System.out.println("被代理的方法执行了");    }}

2. 创建拦截器类

创建一个拦截器类,继承 net.sf.cglib.proxy.MethodInterceptor 接口,并实现 intercept 方法。拦截器类的作用是拦截目标类的方法调用,并在适当的位置添加自定义逻辑:

package com.cglibdynamicproxy.cglib;import java.lang.reflect.Method;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;public class CglibMethodInterceptor implements MethodInterceptor {    @Override    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {        System.out.println("before");        Object result = null;        try {            result = proxy.invokeSuper(obj, args);        } catch (Exception e) {            System.out.println("异常处理:" + e.getMessage());            throw e;        } finally {            System.out.println("after");        }        return result;    }}

3. 导入并设置CGLib依赖

在项目中添加CGLib依赖,确保类loading能够正确进行。在 pom.xml 中添加依赖:

net.sf.cglib
cglib
3.2.5

4. 创建动态代理

使用 Enhancer 工厂类创建动态代理。设置代理目标类和拦截器回调函数,然后创建代理对象:

public static void main(String[] args) {    Enhancer enhancer = new Enhancer();    enhancer.setSuperclass(AgentTarget.class);    enhancer.setCallback(new CglibMethodInterceptor());    AgentTarget childAgentTarget = (AgentTarget) enhancer.create();    childAgentTarget.proxyMethod();}

5. 检查生成的代理类

通过查看生成的 AgentTarget$$EnhancerByCGLIB$$303de0ca 类,可以看到代理类是如何继承和重写目标类方法的。你可以看到生成类中包含了目标类的所有方法,以及拦截器在方法调用时所执行的逻辑。

通过以上步骤,可以清晰地看到CGLib动态代理的工作原理,以及它是如何在无修改目标类的情况下,动态地拦截和执行目标方法的。这种方法能够为系统提供灵活的功能扩展,同时保持目标类的原有功能不变。

转载地址:http://xompz.baihongyu.com/

你可能感兴趣的文章
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>