前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot之AOP执行顺序

SpringBoot之AOP执行顺序

作者头像
九转成圣
发布2024-04-10 17:35:03
550
发布2024-04-10 17:35:03
举报
文章被收录于专栏:csdncsdn

SpringBoot之AOP执行顺序

执行顺序

提示

  1. 不管目标方法前还是目标方法后都是Around先执
  2. After一定执行,AfterReturning与AfterThrowing二选一

代码示例

代码语言:javascript
复制
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
代码语言:javascript
复制
@Aspect
@Component
public class HelloAop {
    @Pointcut("execution(* com.example.hello.controller.HelloController.hello(..))")
    public void pointcut() {

    }

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("@Before");
    }

    @AfterThrowing("pointcut()")
    public void afterThrowing() {
        System.out.println("@AfterThrowing");
    }

    @AfterReturning("pointcut()")
    public void afterReturning() {
        System.out.println("@AfterReturning");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("@After");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object result;
        try {
            System.out.println("@Around...方法执行前");
            result = pjp.proceed();
            System.out.println("@Around...方法执行后");
        } catch (Throwable throwable) {
            System.out.println("@Around...抛出异常后");
            throw throwable;
        } finally {
            System.out.println("@Around...finally");
        }
        return result;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot之AOP执行顺序
  • 执行顺序
  • 提示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档