同一個切點方法有兩個切面下的執行情況


切面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塊仍然會執行。
 
從源碼看執行順序:
TODO


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM