news 2026/6/4 2:55:23

(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/31 12:14:55

【鲲苍提效】一键批量接入外部应用监控,全面提升监控接入效率

汉得鲲苍基础架构管理平台的核心目标是为企业的异构系统提供简单高效的一站式统一闭环管理能力,包括统一资源(集群、主机、存储等)管理、统一应用及部署管理、统一监控管理、统一服务治理,帮助企业实现更快、更好、更全面的异构系…

作者头像 李华
网站建设 2026/6/1 9:55:19

4、索引有哪几种类型?

主键索引: 数据列不允许重复,不允许为NULL,一个表只能有一个主键。唯一索引: 数据列不允许重复,允许为NULL值,一个表允许多个列创建唯一索引。可以通过 ALTER TABLE table_name ADD UNIQUE (column); 创建唯一索引可以通过 ALTER …

作者头像 李华
网站建设 2026/5/29 9:31:40

一篇看懂JWT:Web安全的“身份证”

诸神缄默不语-个人技术博文与视频目录 文章目录 什么是JWT?一个简单的比喻为什么需要JWT?JWT长什么样?1. 头部(Header)2. 载荷(Payload)3. 签名(Signature) 用Python玩转…

作者头像 李华
网站建设 2026/5/20 14:38:06

基于微信小程序的直播带货商品数据分析系统毕设源码+文档+讲解视频

前言 本课题聚焦直播带货行业的数据化运营需求,针对当前直播商品数据分散、分析维度单一、运营决策缺乏精准数据支撑等痛点,设计开发基于微信小程序的直播带货商品数据分析系统。系统以微信小程序为核心载体,结合前端原生开发技术与后端数据处…

作者头像 李华
网站建设 2026/5/30 18:07:38

基于 S7 - 1200 和博图 15.1 的三层立体车库 PLC 设计

三层立体车库plc s7-1200 博图15.1 1、设置启动、停止按钮,且设置指示灯显示车库的开关状态; 2、7个车位的车俩可以自由存取,且车库可以实现自动存取(存取选择最优路径); 3、每个车位均有电机控制&#…

作者头像 李华
网站建设 2026/5/30 18:07:17

当图像开始跳舞:用Matlab玩转频率域滤镜

标题:基于matlab的理想滤波器和巴特沃斯滤波器设计 关键词:一阶巴特沃斯滤波器 理想滤波器 二维傅里叶变换 fft2 描述:对任意图像进行灰度化,然后进行二维傅里叶变化,设定一个阈值,对高频或者低频信号进行滤…

作者头像 李华