Springboot3中aop几个通知注解执行的先后顺序

1、简介

        随着Spring框架的不断更新迭代,在面向切面编程中,Spring AOP使用 @Around(在方法执行前后) 、@Before(在方法执行前)、 @AfterReturning(未抛异常) 、 @After(不论是否抛异常) 、 @Around (在方法执行后)

2、注解描述

注解描述
@Before在代理方法执行前执行
@After在代理方法执行后执行
@AfterReturning在代理方法执行返回后执行
@AfterThrowing在代理方法抛异常时执行
@Around包围着代理方法执行
@Pointcut定义切面

3、@Pointcut注解的使用

        @Pointcut中可以指定execution表达式和其他例如args、within、target等表达式,execution 最为常用,其使用具体模式如下:

execution(<修饰符模式> <返回类型模式><方法名模式>(<参数模式>)<异常模式>) 
# 返回类型模式、方法名模式和参数模式外,其它项都是可选的
# 示例:在com.test包及其子包下所有Controller中所有方法都是被代理的方法
@Pointcut("execution(public * com.test..*Controller.*(..))")

        表达式中通配符的含义:

.. # 在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。
*  # 配任何数量字符(只能代表一个字符串,用在方法中代表一个任意类型参数)。
+  # 匹配指定类型的子类型;仅能作为后缀放在类型模式后边

 4、通知注解使用@PointCut

 4.1、定义连接点

@Pointcut("execution(public * com.weilong..*Controller.*(..))")
public void controllerPointcut(){}

4.2、通知注解关联连接点

@Before("controllerPointcut()")
@After("controllerPointcut()")
@Around("controllerPointcut()")
@AfterThrowing("controllerPointcut()")
@AfterReturning("controllerPointcut()")

5、测试通知注解执行顺序

5.1、准备测试类

5.1.1、切面类

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Modifier;

@Aspect
@Component
@Slf4j
public class LogAspect1 {

    public LogAspect1(){
        System.out.println("LogAspect");
    }

    // excution表达式标识符解释:execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?) 返回类型模式、方法名模式和参数模式外,其它项都是可选的
    // ..:在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。
    // *:匹配任何数量字符(只能代表一个字符串,用在方法中代表一个任意类型参数)。
    @Pointcut("execution(public * com.test..*Controller.*(..))")
    public void controllerPointcut(){

    }

    // 在sprintboot3中aop注解执行顺序:
    // @Around (在方法执行前) -> @Before -> @AfterReturning(未抛异常) -> @After(不论是否抛异常) -> @@Around (在方法执行后)
    @Before("controllerPointcut()")
    public void doBefore(JoinPoint joinPoint){
        log.info(" @Before...");
    }

    // ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中
    @Around("controllerPointcut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // Object proceed() throws Throwable //执行目标方法
        // Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法
        log.info(" @Around...方法前");
        Object result = proceedingJoinPoint.proceed();
        log.info(" @Around...方法后");
        return result;
    }
    @After("controllerPointcut()")
    public void after(JoinPoint joinPoint){
        log.info(" @After...");
    }

    @AfterThrowing("controllerPointcut()")
    public void afterThrow(JoinPoint joinPoint){
        log.info(" @AfterThrowing...");
    }

    @AfterReturning("controllerPointcut()")
    public void afterReturn(JoinPoint joinPoint){
        log.info(" @AfterReturning...");
    }
}

 5.1.2、Controller类

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping(value = "/hello")
    public String test(){
        //int i = 1 / 0;  // 测试抛异常放开
        return "hello world!";
    }
}

5.2、代理方法未抛出异常

        执行顺序如下:

4c1128aa43354ec197c935220cb5ed37.png

5.3、代理方法抛出异常

        执行顺序如下:      

34da1ee76c4b4c7c80def556c5c926ca.png

  6、总结

        本文详细介绍了Springboot3中aop在两种情况下通知的执行顺序,关于更多aop深入知识将在后续博文中更新。

 

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知其_所以然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值