同一个切点方法有两个切面下的执行情况
切面1与切面2之间使用@Order注解指定执行顺序,数字小的先执行
切面1:
@Order(1) @Aspect @Component xxx @Around(value = "webPointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { // 方法执行前加上线程号,并将线程号放到线程本地变量中 MDCUtil.init(); LOGGER.error("aspect1-before"); Object result = joinPoint.proceed(); LOGGER.error("aspect1-after"); return result; }
切面2:
@Order(2) @Aspect @Component xxx @Around(value = "webPointCut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { LOGGER.error("aspect2-before"); Object result = joinPoint.proceed(); LOGGER.error("aspect2-after"); return result; }
情况1:方法都顺利执行
运行结果:
2019-11-05 18:24:07.741|http-nio-8080-exec-1|ERROR|edae81477c6440a6b6aba6e3cd380a96|LogAndCheckAspect#around:146|aspect1-before 2019-11-05 18:24:07.741|http-nio-8080-exec-1|ERROR|edae81477c6440a6b6aba6e3cd380a96|CheckAspect#around:90|aspect2-before 2019-11-05 18:24:07.763|http-nio-8080-exec-1|ERROR|edae81477c6440a6b6aba6e3cd380a96|AuthTokenController#getAccessToken:41|切点方法执行 2019-11-05 18:24:08.067|http-nio-8080-exec-1|ERROR|edae81477c6440a6b6aba6e3cd380a96|CheckAspect#around:92|aspect2-after 2019-11-05 18:24:08.067|http-nio-8080-exec-1|ERROR|edae81477c6440a6b6aba6e3cd380a96|LogAndCheckAspect#around:148|aspect1-after
情况2:切点方法出现异常
切面1:
@Around(value = "webPointcut()") public Object around(ProceedingJoinPoint joinPoint) { // 方法执行前加上线程号,并将线程号放到线程本地变量中 MDCUtil.init(); LOGGER.error("aspect1-before"); Object result = null; try { result = joinPoint.proceed(); LOGGER.error("aspect1-after"); } catch(Throwable throwable){ LOGGER.error("aspect1方法执行异常:" + throwable.getMessage(), throwable); } finally { LOGGER.error("aspect1-finally"); } return result; }
切面2:
@Around(value = "webPointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { LOGGER.error("aspect2-before"); Object result = joinPoint.proceed(); LOGGER.error("aspect2-after"); return result; }
运行结果:
2019-11-05 18:51:28.203|http-nio-8080-exec-4|ERROR|c814fc320ef242df9584a6c6a80484ac|LogAndCheckAspect#around:145|aspect1-before 2019-11-05 18:51:28.203|http-nio-8080-exec-4|ERROR|c814fc320ef242df9584a6c6a80484ac|CheckAspect#around:115|aspect2-before 2019-11-05 18:51:28.227|http-nio-8080-exec-4|ERROR|c814fc320ef242df9584a6c6a80484ac|AuthTokenController#getAccessToken:41|切点方法执行 2019-11-05 18:51:28.228|http-nio-8080-exec-4|ERROR|c814fc320ef242df9584a6c6a80484ac|LogAndCheckAspect#around:151|aspect1方法执行异常:/ by zero 2019-11-05 18:51:28.229|http-nio-8080-exec-4|ERROR|c814fc320ef242df9584a6c6a80484ac|LogAndCheckAspect#around:154|aspect1-finally
情况3:切面2在执行切点方法前返回
切面1:
@Around(value = "webPointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { // 方法执行前加上线程号,并将线程号放到线程本地变量中 MDCUtil.init(); LOGGER.error("aspect1-before"); Object result = null; try { result = joinPoint.proceed(); LOGGER.error("aspect1-after"); } finally { LOGGER.error("aspect1-finally"); } return result; }
切面2:
@Around(value = "webPointCut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { LOGGER.error("aspect2-before"); if(true){ LOGGER.info("aspect2-return"); AccessTokenResult result=new AccessTokenResult(); return result; } Object result = joinPoint.proceed(); LOGGER.error("aspect2-after"); return result; }
运行结果:
2019-11-05 18:33:33.391|http-nio-8080-exec-1|ERROR|236f15e68e374bc98c0a7f30964963e7|LogAndCheckAspect#around:145|aspect1-before 2019-11-05 18:33:33.392|http-nio-8080-exec-1|ERROR|236f15e68e374bc98c0a7f30964963e7|CheckAspect#around:91|aspect2-before 2019-11-05 18:33:33.392|http-nio-8080-exec-1|INFO |236f15e68e374bc98c0a7f30964963e7|CheckAspect#around:93|aspect2-return 2019-11-05 18:33:33.392|http-nio-8080-exec-1|ERROR|236f15e68e374bc98c0a7f30964963e7|LogAndCheckAspect#around:149|aspect1-after 2019-11-05 18:33:33.392|http-nio-8080-exec-1|ERROR|236f15e68e374bc98c0a7f30964963e7|LogAndCheckAspect#around:151|aspect1-finally
即:切点方法不会执行,但是切面1的finally块仍然会执行。
注意:切点方法需@ResponseBody注解
从源码看执行顺序:
TODO
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类