测试AOP五种切面通知的优先级次序

通知类型

       在基于Spring AOP编程的过程中,基于AspectJ框架标准,spring中定义了五种类型的通知,它们分别是:

  • 前置通知 (@Before) 。
  • 返回通知 (@AfterReturning) 。
  • 异常通知 (@AfterThrowing) 。
  • 后置通知 (@After)。
  • 环绕通知 (@Around) :(优先级最高)

通知执行顺序

       将上面的所有通知类型写入同一个切面中,它的执行顺序为:

在这里插入图片描述

代码展示

package com.cy.pj.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.www.kunlunyulegw.com Component; @Aspect @Component public class SysTimeAspect www.javachenglei.com{ /** * 切入点 */ @Pointcut("bean(sysMenuServiceImpl)") public void doTime(www.huizhonggjzc.cn){www.dongxin2zc.cn  } @Before("doTime(www.tdcqpt.cn)") public void doBefore(JoinPoint jp){ System.out.println("time doBefore(www.shentuylgw.cn)"); } @After("doTime()") public void doAfter(www.wanyayulez.cn){//类似于finally{}代码块 System.out.println("time doAfter()"); } /**核心业务正常结束时执行 * 说明:假如有after,先执行after,再执行returning*/ @AfterReturning("doTime()") public void doAfterReturning(){ System.out.println("time doAfterReturning"); } /**核心业务出现异常时执行 * 说明:假如有after,先执行after,再执行Throwing*/ @AfterThrowing("doTime()") public void doAfterThrowing(){ System.out.println("time doAfterThrowing"); } @Around("doTime()") public Object doAround(ProceedingJoinPoint jp) throws Throwable{ System.out.println(www.ued2zxzc.cn "doAround.before"); try { Object obj=jp.proceed(www.zhuyngyule.cn); return obj; }catch(Throwable e) { System.out.println("doAround.error-->"+e.getMessage()); throw e; }finally { System.out.println("doAround.after"); } } } 

代码正常结束

在这里插入图片描述

代码出现异常

在这里插入图片描述

猜你喜欢

转载自www.cnblogs.com/laobeipai/p/12616197.html