当前位置: 首页 > news >正文

spring中常见的两种代理模式

🌿 Spring 中的两种常见代理模式

Spring AOP(面向切面编程)在底层主要通过 两种代理方式 来实现:

代理方式依赖机制特点适用场景
JDK 动态代理 java.lang.reflect.Proxy 只能代理 接口 Bean 实现了接口时,默认使用
CGLIB 动态代理 通过生成目标类的 子类 来实现代理 可代理 没有实现接口的类 Bean 没有实现接口时使用

🧩 一、JDK 动态代理

原理

JDK 提供了一个 Proxy 类,可以在运行时为一组接口生成代理类。
Spring 使用它对实现了接口的 Bean 进行代理。

实现逻辑

当你有:

 
public interface UserService {void addUser(); } public class UserServiceImpl implements UserService {public void addUser() {System.out.println("添加用户");} }

Spring 会生成一个代理类:

 
UserService proxy = (UserService) Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(),new Class[]{UserService.class},(proxyObj, method, args) -> {// 前置通知System.out.println("Before method");Object result = method.invoke(new UserServiceImpl(), args);// 后置通知System.out.println("After method");return result;});

特点

  • 必须有接口

  • 生成的代理类在运行时实现了目标接口;

  • 性能略高(JDK17+版本优化较多);

  • 代理对象类型是 接口类型,而不是目标类类型。


🧬 二、CGLIB 动态代理

原理

通过继承目标类,重写其中的方法,并在方法前后织入增强逻辑。

Spring 使用 org.springframework.cglib.proxy.Enhancer 来生成子类。

示例

 
Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(UserService.class); enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {System.out.println("Before method");Object result = proxy.invokeSuper(obj, args);System.out.println("After method");return result; }); UserService proxy = (UserService) enhancer.create(); proxy.addUser();

特点

  • 适用于 没有实现接口的类

  • 通过继承实现代理;

  • 不能代理 final 类或 final 方法

  • 性能在 JDK17 前通常略优于 JDK 动态代理。


⚙️ 三、Spring 如何选择代理方式

Spring 默认策略(AOP ProxyFactory):

  1. 如果目标类实现了接口 → 使用 JDK 动态代理;

  2. 如果目标类没有实现接口 → 使用 CGLIB 动态代理;

  3. 你也可以强制使用 CGLIB:

 
spring:aop:proxy-target-class: true

或 Java 配置:

 
@EnableAspectJAutoProxy(proxyTargetClass = true)

💡 四、面试高频问法与答题技巧

✅ 常见问法

面试官:Spring AOP 用的是什么代理机制?区别是什么?

标准回答:

Spring AOP 底层主要使用两种代理方式:
当目标类实现了接口时,Spring 默认使用 JDK 动态代理;
当目标类没有实现接口时,使用 CGLIB 动态代理。
两者的区别是 JDK 代理基于接口实现,CGLIB 通过生成子类来代理。


✅ 进阶问法

你知道怎么强制使用 CGLIB 吗?

回答:

可以在配置类上加上 @EnableAspectJAutoProxy(proxyTargetClass = true)
或在配置文件中设置 spring.aop.proxy-target-class=true


✅ 再进阶问法

为什么 Spring Boot 默认开启 CGLIB?

回答:

因为很多 Spring Bean 没有接口(比如 Controller、Service),
而 CGLIB 可以代理没有接口的类,更通用。


✅ 面试陷阱

final 类、final 方法可以被 Spring 代理吗?

回答:

JDK 动态代理不受影响;
但 CGLIB 由于基于继承,无法代理 final 类或 final 方法


🧠 五、扩展:AOP 底层调用链

 
@Aspect -> Advisor -> ProxyFactory -> AopProxyFactory -> JdkDynamicAopProxy / CglibAopProxy -> 织入横切逻辑

📘 总结对比表

项目JDK 动态代理CGLIB 动态代理
原理 反射生成实现接口的代理类 继承目标类生成子类
要求 必须有接口 无接口也可
性能 JDK17起较优 JDK8时代较优
代理类型 接口类型 目标类子类
限制 final类/方法不能代理
Spring默认 有接口时默认 无接口时使用

 

http://www.hskmm.com/?act=detail&tid=39779

相关文章:

  • 在AI技术唾手可得的时代,挖掘新需求成为核心竞争力——某知名数字货币钱包需求洞察
  • What versions of Python still work in Windows XP?
  • SAM2 图像分割(3)鼠标选择多框 摄像头实时分割显示 - MKT
  • Python 内存管理机制与垃圾回收技术解析
  • 随想随说
  • Semantic-SSAM 是“一切多细都行,还能给标签”​​ - MKT
  • 在windows10系统上运行第一个SDL3项目
  • 传统AI模型的垄断壁垒与价值对话范式的演进:一项基于AI元人文构想的博弈格局与路径探析
  • 2025年智能立体库货架厂家推荐排行榜,自动化立体仓库货架,智能仓储货架,重型立体库货架,高位立体库货架公司精选
  • Codeforces Round 1054 (Div. 3) - D、E
  • AI元人文:客观清醒 - 传统模型转型的残酷博弈
  • ​​ORourke 算法​​ 多边形的最小面积外接矩形 - MKT
  • 102302106-陈昭颖-第一次作业
  • P1877 [HAOI2012] 音量调节
  • 数论导论
  • P14321 「ALFR Round 11」D Adjacent Lifting, Fewest Rounds 题解
  • 国庆集训day1~2笔记-动态规划
  • P1679 神奇的四次方数
  • Minio外网访问内网上传的预签名url的方法以及报错原因
  • vscode解决中文乱码
  • 【ESP32 在线语音】星火大模型
  • RT-Thread 之互斥量使用
  • 20232419 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 语义文本理解 BERT - MKT
  • Rig 项目深度分析报告
  • FM-Fusion 利用rgbd相机 ram-GroundingDINO-sam 重建语义地图 - MKT
  • AI元人文构想系列:从战略能力到价值对话的文明之路
  • 事件日志查看Windows安装软件情况
  • RT-Thread之创建线程
  • cias_voice_plyer_handle.c 解析