同一个切点方法有两个切面下的执行情况
切面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