首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >自定义切面编程,执行过程分析Around > Before > Around > After > AfterReturning[AfterThrowing]

自定义切面编程,执行过程分析Around > Before > Around > After > AfterReturning[AfterThrowing]

作者头像
master336
发布2026-06-15 18:46:30
发布2026-06-15 18:46:30
680
举报

AspectJ:面向切面的框架。

主要包括:切面包含切点(Pointcut)及通知(Advice),完整的Aop还应包含连接点(Joint point);

响应顺序:通知响应顺序如下:

通过代码分支得知响应顺序为

Around > Before > Around > After > AfterReturning[AfterThrowing]

说明:

1)上图中三个绿色部分比较特殊这里需要说明一下:

通知里比较特殊的是环绕通知(Arround),环绕通知贯穿切点方法(业务代码),需要通过

代码语言:javascript
复制
    Object o = ProceedingJoinPoint.proceed();

完成切点方法(业务代码)并将其执行结果返回,已保证调用链条继续向下执行。

也就是说, 如果业务代码报错了,ProceedingJoinPoint.proceed()之后的Arround也就不执行了。

2)切点方法(业务代码)执行报错了,并不影响After的执行。

3) 根据切点方法(业务代码)执行过程是否报错,决定执行AfterReturning或者AfterThrowing。

示例代码:

这里只给出Arround的代码

代码语言:javascript
复制
@Around("demoAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{
    System.out.println("Around processed before");
    Object o = joinPoint.proceed();
    System.out.println("Around processed end");
    return o;
}

Arround的使用要求类似Filter,joinPoint.proceed()完成切点方法(业务代码)的继续执行,当发现加入@Arround通知 后无法执行@Before或者业务代码,或者没有返回值时,可以考虑该通知使用有误。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档