news 2026/4/15 13:32:42

Spring AOP原理简析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring AOP原理简析

在 AspectJ用法 里介绍了 spring AOP 的用法。这里以切面LogAspect的生效流程,简单分析下SpringAOP原理。

切面LogAspect 定义如下:

@Aspect @Component public class LogAspect { @Before(value = "execution(String org.example.topic.aop.demo.StudentServiceImpl.*(String))") public void logBeforeProceed(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); String output = String.format("LogAspect.logBefore: methodName=%s, args=%s", methodName, args); System.out.println(output); }

LogAspect类是 Spring AOP(面向切面编程)的一个典型应用,其工作原理涉及 Spring 容器的 Bean 生命周期、代理机制以及 AOP 的织入过程。


1.@Aspect 和 @Component 的作用

@Aspect @Component public class LogAspect { ... }
  • @Component:告诉 Spring 容器这是一个普通的 Spring Bean,Spring 会在启动时通过组件扫描(如@ComponentScan)将其注册为一个 Bean。
  • @Aspect:表明这个类是一个切面(Aspect),即包含通知(Advice)和切点(Pointcut)的逻辑单元。Spring AOP 会特别处理带有@Aspect注解的 Bean。

✅ 所以LogAspect本身就是一个 Spring 管理的 Bean,并且被识别为切面。


2.切点(Pointcut)定义

@Before(value = "execution(String org.example.topic.aop.demo.StudentServiceImpl.*(String))")

这是一个前置通知(Before Advice),其切点表达式含义如下:

  • execution(...):匹配方法执行连接点。
  • String:返回类型必须是String
  • org.example.topic.aop.demo.StudentServiceImpl.*:目标类是StudentServiceImpl,任意方法名(*)。
  • (String):方法参数必须是单个String类型。

✅ 这个切点会匹配StudentServiceImpl中所有返回String、接受一个String参数的方法。


3.Spring 如何创建代理(Proxy)

Spring AOP 是基于代理模式实现的。当容器发现某个 Bean 需要被 AOP 切面增强时,会为其创建一个代理对象,而不是直接返回原始 Bean。

3.1 代理类型选择:
  • 如果目标类(如StudentServiceImpl实现了接口,Spring 默认使用JDK 动态代理(基于接口)。
  • 如果目标类没有实现接口,Spring 会使用CGLIB 代理(通过继承目标类生成子类)。

📌 假设StudentServiceImpl没有实现接口,Spring 会用 CGLIB 生成一个StudentServiceImpl$$EnhancerBySpringCGLIB子类作为代理。

3.2 代理的作用:
  • 当你从 Spring 容器获取StudentServiceImpl的 Bean 时,实际拿到的是代理对象
  • 调用代理对象的方法时,会先经过 AOP 拦截器链(包括你的@Before通知),再调用原始方法。

4.AOP 织入(Weaving)过程

在 Spring 容器启动过程中,会执行以下关键步骤:

  1. Bean 扫描与注册

    • LogAspect@Component注册为 Bean。
    • StudentServiceImpl也被注册为 Bean(假设它也有@Service@Component)。
  2. BeanPostProcessor 处理
    Spring 使用AnnotationAwareAspectJAutoProxyCreator(一个特殊的BeanPostProcessor)来处理 AOP:

    • 它会扫描所有@AspectBean(如LogAspect)。
    • 分析其中的切点表达式。
    • 检查其他普通 Bean(如StudentServiceImpl)是否匹配这些切点。
  3. 创建代理

    • 如果StudentServiceImpl匹配LogAspect中的切点,AnnotationAwareAspectJAutoProxyCreator会在postProcessAfterInitialization阶段替换原始 Bean 为代理对象
  4. 方法调用时的拦截

    • 当你调用studentService.someMethod("hello")时:
      • 实际调用的是代理对象的方法。
      • 代理对象内部会构建一个拦截器链(Interceptor Chain)
      • 首先执行logBeforeProceed(因为是@Before)。
      • 然后调用原始StudentServiceImpl的目标方法。

5.JoinPoint 的作用

public void logBeforeProceed(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); ... }
  • JoinPoint是 AOP 中的“连接点”,代表程序执行过程中的一个点(如方法调用)。
  • Spring AOP 会自动将当前方法调用的上下文(方法名、参数等)封装到JoinPoint对象中,并传入通知方法。

6. 流程总结

Spring 启动 ↓ 扫描 @Component → 注册 LogAspect 和 StudentServiceImpl 为 Bean ↓ AnnotationAwareAspectJAutoProxyCreator 发现 @Aspect ↓ 解析切点表达式:execution(String StudentServiceImpl.*(String)) ↓ 检查 StudentServiceImpl 是否匹配 → 是! ↓ 为 StudentServiceImpl 创建代理(JDK/CGLIB) ↓ 容器中注入的是代理对象 ↓ 调用 studentService.xxx("arg") ↓ 代理拦截 → 执行 LogAspect.logBeforeProceed(JoinPoint) ↓ 输出日志 ↓ 调用原始 StudentServiceImpl.xxx("arg")

补充说明

  • 仅对 Spring Bean 有效:AOP 只对 Spring 容器管理的 Bean 生效。如果new StudentServiceImpl()手动创建对象,AOP 不会生效。
  • 代理限制:CGLIB 无法代理final方法;JDK 代理只能代理接口方法。
  • 性能:AOP 通过动态代理实现,有一定开销,但通常可忽略。

通过以上机制,LogAspect就能在不修改StudentServiceImpl代码的情况下,透明地在方法调用前插入日志逻辑,体现了 AOP 的“横切关注点”分离思想。

7. 相关文档

1. AspectJ用法: https://blog.csdn.net/taotiezhengfeng/article/details/149515395

2. .Spring AOP切点表达式的关键词梳理: https://blog.csdn.net/taotiezhengfeng/article/details/155744545

3. HandlerInterceptor 与 AOP 对比:https://blog.csdn.net/taotiezhengfeng/article/details/155452970

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/11 2:01:05

33、Linux 系统打印与跨平台转换全攻略

Linux 系统打印与跨平台转换全攻略 在 Linux 系统中,打印和跨平台数据处理是常见的操作需求。本文将详细介绍 Linux 系统下的打印方法、文件打印前的准备工作,以及跨平台数据转换的相关技巧。 打印问题解决与多途径打印方法 当打印机出现问题时,可采取以下步骤解决:先将…

作者头像 李华
网站建设 2026/4/11 14:46:31

震惊!云服务器生产商排名大洗牌,这家竟逆袭成黑马!

震惊&#xff01;云服务器生产商排名大洗牌&#xff0c;这家竟逆袭成黑马&#xff01;近年来&#xff0c;全球数字化转型浪潮汹涌澎湃&#xff0c;云计算作为核心基础设施&#xff0c;其市场竞争日趋白热化。传统的市场格局正在被打破&#xff0c;一场深刻的云服务器生产商排名…

作者头像 李华
网站建设 2026/4/8 22:57:02

Cadence Allegro 电子设计 快问快答--05.OrCAD中字体的大小怎么设置?

大家好&#xff0c;本期分享的主题是&#xff1a;在OrCAD中如何设置字体大小。在电路设计与仿真过程中&#xff0c;工程师需要创建清晰易读的原理图&#xff0c;以确保设计的准确性和团队协作的高效性。下面我们分步介绍具体的操作方法&#xff1a;关键操作步骤&#xff1a;打开…

作者头像 李华
网站建设 2026/4/13 20:55:37

40、Linux 网络操作与服务使用指南

Linux 网络操作与服务使用指南 在 Linux 系统中,有许多强大的工具和命令可用于网络操作和使用各种互联网服务。下面将详细介绍这些工具和命令的使用方法。 1. 网页相关操作 1.1 下载文件与查看网页头部信息 使用 wget 命令可以方便地从网络下载文件。例如,使用以下命令…

作者头像 李华