news 2026/5/26 18:13:29

(36)通知与切面

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(36)通知与切面

通知类型

通知类型包括:

  • 前置通知:@Before 目标方法执行之前的通知
  • 后置通知:@AfterReturning 目标方法执行之后的通知
  • 环绕通知:@Around 目标方法之前添加通知,同时目标方法执行之后添加通知。
  • 异常通知:@AfterThrowing 发生异常之后执行的通知
  • 最终通知:@After 放在finally语句块中的通知

接下来,编写程序来测试这几个通知的执行顺序:

packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.stereotype.Component;// 切面类@Component@AspectpublicclassMyAspect{@Around("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println("环绕通知开始");// 执行目标方法。proceedingJoinPoint.proceed();System.out.println("环绕通知结束");}@Before("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidbeforeAdvice(){System.out.println("前置通知");}@AfterReturning("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterReturningAdvice(){System.out.println("后置通知");}@AfterThrowing("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterThrowingAdvice(){System.out.println("异常通知");}@After("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterAdvice(){System.out.println("最终通知");}}
packagecom.powernode.spring6.service;importorg.springframework.stereotype.Component;// 目标类@ComponentpublicclassOrderService{// 目标方法publicvoidgenerate(){System.out.println("订单已生成!");}}
packagecom.powernode.spring6.test;importcom.powernode.spring6.service.OrderService;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassAOPTest{@TestpublicvoidtestAOP(){ApplicationContextapplicationContext=newClassPathXmlApplicationContext("spring-aspectj-aop-annotation.xml");OrderServiceorderService=applicationContext.getBean("orderService",OrderService.class);orderService.generate();}}

执行结果:

通过上面的执行结果就可以判断他们的执行顺序了,这里不再赘述。
结果中没有异常通知,这是因为目标程序执行过程中没有发生异常。我们尝试让目标方法发生异常:

packagecom.powernode.spring6.service;importorg.springframework.stereotype.Component;// 目标类@ComponentpublicclassOrderService{// 目标方法publicvoidgenerate(){System.out.println("订单已生成!");if(1==1){thrownewRuntimeException("模拟异常发生");}}}

再次执行测试程序,结果如下:

通过测试得知,当发生异常之后,最终通知也会执行,因为最终通知@After会出现在finally语句块中。
出现异常之后,后置通知环绕通知的结束部分不会执行。

切面的先后顺序

我们知道,业务流程当中不一定只有一个切面,可能有的切面控制事务,有的记录日志,有的进行安全控制,如果多个切面的话,顺序如何控制:可以使用@Order注解来标识切面类,为@Order注解的value指定一个整数型的数字,数字越小,优先级越高
再定义一个切面类,如下:

packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;@Aspect@Component@Order(1)//设置优先级publicclassYourAspect{@Around("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println("YourAspect环绕通知开始");// 执行目标方法。proceedingJoinPoint.proceed();System.out.println("YourAspect环绕通知结束");}@Before("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidbeforeAdvice(){System.out.println("YourAspect前置通知");}@AfterReturning("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterReturningAdvice(){System.out.println("YourAspect后置通知");}@AfterThrowing("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterThrowingAdvice(){System.out.println("YourAspect异常通知");}@After("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterAdvice(){System.out.println("YourAspect最终通知");}}
packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;// 切面类@Component@Aspect@Order(2)//设置优先级publicclassMyAspect{@Around("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println("环绕通知开始");// 执行目标方法。proceedingJoinPoint.proceed();System.out.println("环绕通知结束");}@Before("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidbeforeAdvice(){System.out.println("前置通知");}@AfterReturning("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterReturningAdvice(){System.out.println("后置通知");}@AfterThrowing("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterThrowingAdvice(){System.out.println("异常通知");}@After("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterAdvice(){System.out.println("最终通知");}}

执行测试程序:

通过修改@Order注解的整数值来切换顺序,执行测试程序:

优化使用切点表达式

观看以下代码中的切点表达式:

packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;// 切面类@Component@Aspect@Order(2)publicclassMyAspect{@Around("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println("环绕通知开始");// 执行目标方法。proceedingJoinPoint.proceed();System.out.println("环绕通知结束");}@Before("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidbeforeAdvice(){System.out.println("前置通知");}@AfterReturning("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterReturningAdvice(){System.out.println("后置通知");}@AfterThrowing("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterThrowingAdvice(){System.out.println("异常通知");}@After("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidafterAdvice(){System.out.println("最终通知");}}

缺点是:

  • 第一:切点表达式重复写了多次,没有得到复用。
  • 第二:如果要修改切点表达式,需要修改多处,难维护。

可以这样做:将切点表达式单独的定义出来,在需要的位置引入即可。如下:

packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;// 切面类@Component@Aspect@Order(2)publicclassMyAspect{@Pointcut("execution(* com.powernode.spring6.service.OrderService.*(..))")publicvoidpointcut(){}@Around("pointcut()")publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println("环绕通知开始");// 执行目标方法。proceedingJoinPoint.proceed();System.out.println("环绕通知结束");}@Before("pointcut()")publicvoidbeforeAdvice(){System.out.println("前置通知");}@AfterReturning("pointcut()")publicvoidafterReturningAdvice(){System.out.println("后置通知");}@AfterThrowing("pointcut()")publicvoidafterThrowingAdvice(){System.out.println("异常通知");}@After("pointcut()")publicvoidafterAdvice(){System.out.println("最终通知");}}

使用@Pointcut注解来定义独立的切点表达式。
注意这个@Pointcut注解标注的方法随意,只是起到一个能够让@Pointcut注解编写的位置。
执行测试程序:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/23 10:05:56

飞剪追剪程序plc程序伺服程序 同步控制 适合新手学习参考 包含PLC程序+触摸屏程序+CAD...

飞剪追剪程序plc程序伺服程序 同步控制 适合新手学习参考 包含PLC程序触摸屏程序CAD电路图纸。飞剪追剪这玩意儿听起来高端,实际咱们拆开来看其实挺有意思的。新手搞这个别慌,先说清楚核心就四个字:同步控制。说白了就是让刀片追上材料运动轨…

作者头像 李华
网站建设 2026/5/19 0:32:55

5、索引的数据结构(b+树,hash)

索引的数据结构(b树,hash) 索引的数据结构和具体存储引擎的实现有关,在MySQL中使用较多的索引有Hash索引,B树索引等,而我们经常使用的InnoDB存储引擎的默认索引实现为:B树索引。 对于哈希索引…

作者头像 李华
网站建设 2026/5/24 11:55:26

【课程设计/毕业设计】基于Java+SpringBoot+VUE的旅游信息分享管理平台系统基于VUE的旅游信息分享管理平台【附源码、数据库、万字文档】

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华
网站建设 2026/5/22 0:52:44

Java计算机毕设之基于VUE的旅游信息分享管理平台基于Springboot+Vue的旅游攻略分享平台系统(完整前后端代码+说明文档+LW,调试定制等)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华
网站建设 2026/5/12 2:43:32

(新卷,200分)- 找单词(Java JS Python)

(新卷,200分)- 找单词(Java & JS & Python) 题目描述 给一个字符串和一个二维字符数组,如果该字符串存在于该数组中,则按字符串的字符顺序输出字符串每个字符所在单元格的位置下标字符串,如果找不到返回字符…

作者头像 李华