当前位置:首页 > CN2资讯 > 正文内容

Java:SpringBoot实现JDK动态代理和CGLIB动态代理

3天前CN2资讯



目录


需要代理的对象

// 接口 public interface PayService { void pay(); } // 实现 public class AliPayService implements PayService { @Override public void pay() { System.out.println("AliPayService"); } }

1. JDK 动态代理

在 JDK 动态代理中,被代理类必须实现一个或多个接口,并通过 InvocationHandler 接口来实现代理类的具体逻辑。

实现 InvocationHandler

package com.example.demo.proxy.handler; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class LogInvocationHandler implements InvocationHandler { private Object target; public LogInvocationHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("log before invoke"); Object result = method.invoke(this.target, args); System.out.println("log after invoke"); return result; } }

测试类

package com.example.demo.proxy; import com.example.demo.proxy.handler.LogInvocationHandler; import com.example.demo.proxy.impl.AliPayService; import org.junit.Test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class InvocationHandlerTests { @Test public void testLogInvocationHandler() { PayService payService = new AliPayService(); // 创建一个代理类:通过被代理类、被代理实现的接口、方法调用处理器来创建 PayService payServiceProxy = (PayService) Proxy.newProxyInstance( payService.getClass().getClassLoader(), new Class[]{PayService.class}, new LogInvocationHandler(payService) ); payServiceProxy.pay(); // log before invoke // AliPayService // log after invoke } }

2. CGLIB 动态代理

CGLIB 动态代理是一种使用 CGLIB 库来实现动态代理的技术。在 CGLIB 动态代理中,代理类不需要实现接口,而是通过继承被代理类来实现代理。

package com.example.demo.proxy.handler; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class LogMethodInterceptor implements MethodInterceptor { // 被代理对象 private Object target; public LogMethodInterceptor(Object target) { this.target = target; } @Override public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("log before invoke"); Object result = method.invoke(this.target, args); System.out.println("log after invoke"); return result; } }

测试类

package com.example.demo.proxy; import com.example.demo.proxy.handler.LogMethodInterceptor; import com.example.demo.proxy.impl.AliPayService; import org.junit.Test; import org.springframework.cglib.proxy.Enhancer; public class LogMethodInterceptorTests { @Test public void testLogMethodInterceptor() { PayService target = new AliPayService(); PayService proxy = (PayService) Enhancer.create( target.getClass(), new LogMethodInterceptor(target) ); proxy.pay(); // log before invoke // AliPayService // log after invoke } }

总结

实现方法

实现技术

JDK 动态代理

实现接口

CGLIB 动态代理

继承父类

参考文章

  • 动态代理是如何实现的?
  • JDK动态代理和CGLIB有什么区别?


    • 你可能想看:

      扫描二维码推送至手机访问。

      版权声明:本文由皇冠云发布,如需转载请注明出处。

      本文链接:https://www.idchg.com/info/24737.html

      分享给朋友:

      “Java:SpringBoot实现JDK动态代理和CGLIB动态代理” 的相关文章

      如何找到便宜域名?免费与低价域名注册全攻略

      知名免费二级域名介绍 在寻找免费域名时,有几个知名的选项值得考虑。eu.org 是一个长期存在的免费二级域名,支持DNSSEC,适合部署在Vercel等平台上。不过,它在中国某些地区可能会遇到访问限制,且申请时间不确定。US.KG 是另一个热门选择,可以接入Cloudflare,但需要有一个活跃的G...

      搬瓦工(BandwagonHost)VPS指南:选择适合你的方案与实用技巧

      搬瓦工(BandwagonHost)是一家在VPS领域颇具声望的提供商。作为杭州的一家企业,搬瓦工致力于为用户提供高性价比的虚拟专用服务器服务,在业内吸引了大量的关注和客户。 说到搬瓦工的优势,机房和线路的多样性是一个重要因素。它在多个国家设有数据中心,包括香港、美国和日本等地。这种广泛的选择使用户...

      外网域名哪里最便宜?选择最佳注册商的指南

      1.1 什么是外网域名注册商? 外网域名注册商是提供域名注册服务的公司,它们通常允许用户选择和注册自己希望拥有的网站地址。这些注册商的作用不仅仅是处理注册申请。它们还提供一系列相关服务,例如域名转移、续费、DNS管理以及隐私保护等。选择合适的注册商对于建立网站而言非常重要,它直接影响到域名的成本和后...

      Putty教程:从安装到高级功能的全面指南

      什么是Putty? Putty是一款广泛使用的终端仿真器,它主要用于与远程设备进行连接。通过Putty,用户能够在本地计算机上访问多种类型的网络设备和服务器。它的界面简单明了,尤其适合新手使用,后期又具备足够的扩展功能来满足高级用户的需求。每当我需要管理远程服务器时,Putty总是我首选的工具。 我...

      香港服务器推荐:选择最适合你的服务器类型与服务商

      在数字化时代,服务器的选择对企业和个人用户来说都是一项重要决策。今天,我想和大家聊聊香港服务器。这些服务器以其独特的地理位置和技术优势,成为了许多用户的首选。特别是如果你的目标用户主要在中国大陆和东南亚地区,选择香港服务器无疑是个明智的选择。 首先,香港作为一个国际化大都市,其网络基础设施非常发达。...

      如何选择和使用CN2GIA VPS提升网络体验

      当我第一次接触CN2GIA VPS的时候,心中充满了好奇。CN2GIA VPS是一种虚拟专用服务器,特别适合需要高效稳定网络连接的用户。它的全称是“China Telecom CN2 GIA”,其中“CN2”代表中国电信的第二代网络,而“GIA”则表示全球互联网接入。这种技术通过优化网络路径,提供了...