news 2026/4/16 8:13:49

(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/4/15 17:59:58

微信小程序vue_uniapp研究生导师日常交互师生交流,考勤打卡任务,请假

文章目录具体实现截图主要技术与实现手段系统设计与实现的思路系统设计方法java类核心代码部分展示结论源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式!具体实现截图 本系统(程序源码数据库调试部署讲解)带文档1万…

作者头像 李华
网站建设 2026/4/15 12:45:56

6、索引算法有哪些?

索引算法有哪些&#xff1f;索引算法有 BTree算法和Hash算法BTree算法BTree是最常用的mysql数据库索引算法&#xff0c;也是mysql默认的算法。因为它不仅可以被用在,>,>,<,<和between这些比较操作符上&#xff0c;而且还可以用于like操作符&#xff0c;只要它的查询…

作者头像 李华
网站建设 2026/4/3 5:47:33

7、索引设计的原则

索引设计的原则适合索引的列是出现在where子句中的列&#xff0c;或者连接子句中指定的列基数较小的类&#xff0c;索引效果较差&#xff0c;没有必要在此列建立索引使用短索引&#xff0c;如果对长字符串列进行索引&#xff0c;应该指定一个前缀长度&#xff0c;这样能够节省大…

作者头像 李华
网站建设 2026/4/10 15:20:03

Java毕设选题推荐:基于Springboot+Vue的旅游攻略分享平台系统基于VUE的旅游信息分享管理平台【附源码、mysql、文档、调试+代码讲解+全bao等】

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

作者头像 李华
网站建设 2026/4/12 18:13:14

c语言之utf8转unicdoe

static int utf8_to_unicode(unsigned char *in, unsigned short *out, int *outsize) {unsigned

作者头像 李华