spring aop切面执行顺序

article/2025/9/21 14:20:19

spring aop切面执行顺序

     

             

                              

切面执行顺序

       

现有切面1、切面2对同一个切点按先后顺序执行(切面1先于切面2执行)

切面1:@Before、@After、@AfterReturnning、@AfterThrowing、@Around 执行前、@Around 正常执行、@Around 异常执行、@Around 执行后切面2:@Before、@After、@AfterReturnning、@AfterThrowing、@Around 执行前、@Around 正常执行、@Around 异常执行、@Around 执行后

         

切点正常执行

# 切点执行前
切面1:@Around 执行前、@Before
切面2:@Around 执行前、@Before切点执行# 切点执行后
切面2:@AfterReturning、@After、@Around 正常执行、@Around 执行后
切面1:@AfterReturning、@After、@Around 正常执行、@Around 执行后

      

切点异常执行

# 切点执行前
切面1:@Around 执行前、@Before
切面2:@Around 执行前、@Before切点执行# 切点执行后
切面2:@AfterThrowing、@After、@Around 异常执行、@Around 执行后
切面1:@AfterThrowing、@After、@Around 异常执行、@Around 执行后

       

              

                              

使用示例

            

                         

            

HelloService

public interface HelloService {String hello();
}

      

HelloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {@Overridepublic String hello() {return "hello";}
}

       

CustomAspect

@Aspect
@Order(-2)
@Component
public class CustomAspect {@Pointcut("execution(* *.hello()) && within(com.example.demo.service..*)")public void fun(){}@Before("fun()")public void before(){System.out.println("CustomAspect before");}@After("fun()")public void after(){System.out.println("CustomAspect after");}@AfterReturning("fun()")public void afterReturning(){System.out.println("CustomAspect afterReturning");}@AfterThrowing("fun()")public void afterThrowing(){System.out.println("CustomAspect afterThrowing");}@Around("fun()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable{Object result = null;System.out.println("CustomAspect around执行前");try {result = joinPoint.proceed();System.out.println("CustomAspect around执行后正常返回");}catch (Throwable e){e.printStackTrace();System.out.println("CustomAspect around执行后抛出异常");throw e;}finally {System.out.println("CustomAspect around执行后");}return result;}
}

        

CustomAspect2

@Aspect
@Order(-1)
@Component
public class CustomAspect2 {@Pointcut("execution(* *.hello()) && within(com.example.demo.service..*)")public void fun(){}@Before("fun()")public void before(){System.out.println("CustomAspect2 before");}@After("fun()")public void after(){System.out.println("CustomAspect2 after");}@AfterReturning("fun()")public void afterReturning(){System.out.println("CustomAspect2 afterReturning");}@AfterThrowing("fun()")public void afterThrowing(){System.out.println("CustomAspect2 afterThrowing");}@Around("fun()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable{Object result = null;System.out.println("CustomAspect2 around执行前");try {result = joinPoint.proceed();System.out.println("CustomAspect2 around执行后正常返回");}catch (Throwable e){e.printStackTrace();System.out.println("CustomAspect2 around执行后抛出异常");throw e;}finally {System.out.println("CustomAspect2 around执行后");}return result;}
}

      

HelloController

@RestController
public class HelloController {@Resourceprivate HelloService helloService;@RequestMapping("/hello")public String hello(){return helloService.hello();}
}

       

            

                              

使用测试

   

localhost:8080/hello,控制台输出:

CustomAspect around执行前
CustomAspect before
CustomAspect2 around执行前
CustomAspect2 before
CustomAspect2 afterReturning
CustomAspect2 after
CustomAspect2 around执行后正常返回
CustomAspect2 around执行后
CustomAspect afterReturning
CustomAspect after
CustomAspect around执行后正常返回
CustomAspect around执行后

               

                      


http://chatgpt.dhexx.cn/article/TgFkhH7S.shtml

相关文章

一文带你搞定AOP切面

摘要:AOP在spring中又叫“面向切面编程”,是对传统我们面向对象编程的一个补充,主要操作对象就是“ 切面”, 可以简单的理解它是贯穿于方法之中,在方法执行前、执行时、执行后、返回值后、异常后要执行的操作。 本文分…

SpringBoot AOP切面实现

文章目录 一、AOP简介二、AOP体系与概念三、AOP实例1、创建SpringBoot工程2、添加依赖3、AOP相关注解3.1、Aspect3.2、Pointcut3.2.1、execution()3.2.2、annotation() 3.3、Around3.4、Before3.5、After3.6、AfterReturning3.7、AfterThrowing 一、AOP简介 AOP(As…

AOP切面编程的理解

一、什么是Spring的AOP? AOP在spring中又叫“面向切面编程”,它可以说是对传统我们面向对象编程的一个补充,从字面上顾名思义就可以知道,它的主要操作对象就是“切面”,所以我们就可以简单的理解它是贯穿于方法之中&a…

AOP切面使用

一、主要设计注解&#xff1a; Aspect After before Pointcut Around pom文件引入 <!--用于aop切面编程--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> 二、AOP核心…

AOP面向切面

1.什么是Spring的AOP? AOP又叫"面向切面编程",是对传统的面向对象编程的一个补充,主要的操作对象就是"切面 ",可以简单的理解它是贯穿于方法之中,在方法执行前、执行时、执行后、返回值后、异常后要执行的操作。 相当于将我们原本一条线执行的程序在中间切…

【JavaEE】Spring AOP (面向切面)详解

目录&#xff1a; 1. 什么是 Spring AOP&#xff1f;1.1 AOP1.2 使用 AOP 的场景 2. AOP 组成2.1 切面&#xff08;Aspect&#xff09;2.2 连接点&#xff08;Join Point&#xff09;2.3 切点&#xff08;Pointcut&#xff09;2.4 通知&#xff08;Advice&#xff09; 3. AOP 概…

斜杠,反斜杠说明

/ 斜杠 \反斜杠 在window中都用斜杠 反斜杠是用来转译字符串的 eg: \"a\" 输出"a"

斜杠、反斜杠、双斜杠、反双斜杠的区别和使用方法及范围

背景 这边我就找了两篇大神写的文章&#xff0c;讲得非常清晰明了。文章主要讲了一些历史缘故和我们面对各种斜杠时的疑惑。 斜杠’/’ 和反斜杠’’ 深入探讨正斜杠和反斜杠 概念 1. 斜杠"/"是URL地址中用到的分隔符&#xff0c;并且在linux系统中的文件路径也是…

glob.glob()之返回路径的正反斜杆问题

Windows环境下用一个反斜杠就行 绝对路径&#xff1a;D:\PyCharm_code\pytorch_study\xxx 相对路径&#xff1a;.\cifar10_train\**\*.png 以下是踩过的坑&#xff1a;记录下

正反斜杠的区别_正斜杠( / )和反斜杠( \ )的区别

反斜杠“\”是电脑出现了之后为了表示程序设计里的特殊含义才发明的专用标点。所以除了程序设计领域外&#xff0c;任何地方都不应该使用反斜杠。 如何区分正反斜杠 英语&#xff1a;"/" 英文是forward slash, “\" 是backward slash 形象些比喻的话&#xff0c…

正斜杠 “/” 与反斜杠 “\”辨析

文章目录 1. 正斜杠 / / /2. 反斜杠 \ \backslash \3. 正斜杠与反斜杠的区别4. 注意 注意&#xff0c; / / / 为正斜杠(forward slash)&#xff0c;而 \ \backslash \ 为反斜杠(backward slash)。 1. 正斜杠 / / / 斜线是斜线标点符号 / / /。曾经用于标记句点和逗号的斜…

斜杠'/' 和反斜杠'\'

斜杠’/‘和反斜杠’\’ 2019-1-21 引言&#xff1a;从大一进入信息专业&#xff0c;正式接触计算机、代码也有几年了。一开始迷迷糊糊学Ascii码&#xff0c;很多特殊字符都需要转义&#xff0c;比如换行符\n&#xff0c;自那时起我就拎不清转义符是斜杠还是反斜杠&#xff0c;…

全面了解 Python 中的反斜杆

本文全面介绍了 Python 中反斜杆(\)的用法&#xff0c;包括原始字符串和普通字符串&#xff0c;repr() 和 str() ,\ 作为转义符&#xff0c;\ 作为续行符&#xff0c;\ 在字符串转义和正则表达式转义中的过程及注意事项等。阅读本文预计 6 min. 全面了解 Python 中的反斜杆 1. …

教你认识正斜杠(/)与反斜杠(\)

正斜杠&#xff0c;又称左斜杠&#xff0c;符号是 “/” ; 反斜杠&#xff0c;也称右斜杠&#xff0c;符号是 “” 。 经常很迷惑正斜杠与反斜杠到底有何区别&#xff1f;以下是一些总结: 背景了解 &#xff1a; DOS路径&#xff1a; C:\WINDOWS\SETTING …这是反斜杠的作用后…

微信支付,二维码图片解析

微信支付&#xff1a; 后台返回的是数据流&#xff1b; 开始这样&#xff0c;但是不行&#xff0c; 解决&#xff1a;在请求里面加入 ‘responseType’: ‘blob’ , 转换&#xff1a;附上base64转图片 //base64转换base64ImgtoFile(dataurl, filename file) {let arr data…

Apache里如何将图片解析成PHP

首先&#xff0c;如果没有安装PHP&#xff0c;先安装PHP yum install -y php然后进入网站根目录&#xff0c;如果不记得网站根目录&#xff0c;可以去配置文件里找 我的是/mnt/z 所以进入这个目录下&#xff0c;新建一个i.jpg文件 在浏览器里查看这个文件&#xff0c;存在错误…

图像解析力算法—SFR(Spatial Frequency Response)

Mitre SFR 1.4和sfrmat3是基于ISO 12233标准&#xff0c;但在某些方面彼此不同&#xff1a;Mitre SFR 1.4旨在尽可能接近标准&#xff0c; 而sfrmat3包含一些改进&#xff0c;可以获得精确的结果 即使被测试的图像质量极低。 M O R E MTF50&#xff0c;MTF50P 在表示相机图像…

JPEG图像格式解析

参考链接&#xff1a;jpeg图片格式详解_460833359的博客-CSDN博客_jpg文件通常是什么 一、JPEG图像介绍 jpg/jpeg是24位的图像文件格式&#xff0c;也是一种高效率的压缩格式&#xff0c;文件格式是JPEG&#xff08;联合图像专家组&#xff09;标准的产物&#xff0c;是面向连…

图像解析力算法—SFR(Spatial Frequency Response)概念理解

最近这一个月在搞SFR算法--&#xff08;空间频域响应&#xff09;&#xff0c;终于也算是搞出来了&#xff0c;网上关于SFR计算MTF的资料和博客也是比较少&#xff0c;现在就是总结一下&#xff0c;也算是方便后人&#xff0c;篇幅估计会比较长&#xff0c;会分篇慢慢写。 讲到…

DXF解析CAD图形解析PLT格式文件解析C#工程源码

DXF解析CAD图形解析PLT格式文件解析C#工程源码 激光切割机 雕刻机 打标机 写字机 巡边机 1.在文件菜单中选择打开dxf文件&#xff0c;算法会自动解析图形 2.解析完成后自动还原图形在界面显示 3.图形中的线条左边自动保存&#xff0c;在界面右侧工具栏选择开始加工按钮&…