同一个切点方法有两个切面下的执行情况

切面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
posted @   杨岂  阅读(2736)  评论(0编辑  收藏  举报
编辑推荐:
· .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技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示