Spring AOP相关注解及执行顺序

@Aspect(切面):用于标识一个类是切面的注解。通常与其他通知注解一起使用,定义切面类。

@Pointcut(切点): 注解来定义切点,它用于描述哪些连接点将会被通知所通知。

连接点:execution(* com.example.service.*.*(..))

通知类

  1. @Before:前置通知,在目标方法执行前执行。
  2. @Around:环绕通知,在目标方法执行前后都执行,并且可以控制是否执行目标方法。
  3. @AfterReturning:正常返回通知,目标方法正常返回后执行。
  4. @AfterThrowing:异常返回通知,在目标方法抛出异常后执行。
  5. @After:后置通知,在目标方法执行后执行,无论是否抛出异常都会执行。

执行顺序:

1、使用前置通知

  • 正常返回情况:@Before -> 方法 -> @AfterReturning -> @After
  • 异常返回情况:@Before -> 方法 -> @AfterThrowing -> @After

2、使用环绕通知

  • 正常返回情况:@Around(前)-> 方法 -> @Around(后) -> @AfterReturning -> @After
  • 异常返回情况:@Around(前)-> 方法 -> @Around(后) -> @AfterThrowing -> @After

3、前置通知和环绕通知都使用

  • 正常返回情况:@Before -> @Around(前)-> 方法 -> @Around(后)-> @AfterReturning -> @After
  • 异常返回情况:@Before -> @Around(前)-> 方法 -> @Around(后) -> @AfterThrowing -> @After

注意:在 @Around 通知类型中,通过调用 ProceedingJoinPoint.proceed() 才会触发目标方法的执行,因此可以在方法执行前后加入额外的逻辑。

实现AOP案例:

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.MyService.*(..))")
    public void beforeMethodExecution(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Before executing method: " + methodName);
    }

    @Around("execution(* com.example.MyService.*(..))")
    public Object aroundMethodExecution(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        String methodName = proceedingJoinPoint.getSignature().getName();
        System.out.println("Before executing method: " + methodName);

        // 执行目标方法
        Object result = proceedingJoinPoint.proceed();

        System.out.println("After executing method: " + methodName + ", result is " + result);

        return result;
    }

    @AfterReturning(pointcut = "execution(* com.example.MyService.*(..))", returning = "result")
    public void afterMethodExecution(Object result) {
        System.out.println("After method execution, the result is " + result);
    }

    @AfterThrowing(pointcut = "execution(* com.example.MyService.*(..))", throwing = "e")
    public void afterMethodThrowing(JoinPoint joinPoint, Exception e) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Method " + methodName + " threw exception: " + e.getMessage());
    }

    @After("execution(* com.example.MyService.*(..))")
    public void afterMethodExecution(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("After executing method: " + methodName);
    }
}

ps:以下是我整理的java面试资料,感兴趣的可以看看。最后,创作不易,觉得写得不错的可以点点关注!

链接:https://www.yuque.com/u39298356/uu4hxh?# 《Java面试宝典》 

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值