一.切入点Pointcut
二.Advide执行顺序
- @Around
- @Before
- @AfterReturning
- @AfterThrowing
- @After
@Pointcut("execution(public * com..*.controller.*.*(..))")
public void controllerPointcut() {
}
@Around("controllerPointcut()")
private void doAround(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("====== 1 Around start ======");
joinPoint.proceed();
log.info("====== 2 Around end ======");
}
@Before("controllerPointcut()")
private void doBefore(JoinPoint joinPoint) {
log.info("====== 2 doBefore ======");
}
@AfterReturning(value = "controllerPointcut()", returning = "keys")
public void doAfterReturning(JoinPoint joinPoint, Object keys) {
log.info("====== 3-1 doAfterReturning{} ======", keys);
}
@AfterThrowing(pointcut = "controllerPointcut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
log.info("====== 3-2 doAfterThrowing ======");
}
@After("controllerPointcut()")
public void doAfter(JoinPoint joinPoint) {
log.info("====== 4 doAfter ======");
}
如果没有@Around:1.先执行@Before,再执行真正的controller方法。2.有异常执行@AfterThrowing,没有异常执行@AfterReturning,@AfterThrowing和@AfterReturning只执行其中一个。3.最后执行@After。
如果有@Around:在Around方法种需要有joinPoint.proceed(),且该行及之前的最先执行。2.执行完joinPoint.proceed();才进入@Before,流程和没有@Around一致。如果方法执行过程没有异常,执行完@After最后后执行@Around中 joinPoint.proceed() 之后的代码。如果有异常,@Around中 joinPoint.proceed() 之后的方法不再执行。
正常返回结果
====== 1 Around start ======
====== 2 doBefore ======
====== 3-1 doAfterReturningCommonResult(code=-1) ======
====== 4 doAfter ======
====== 2 Around end ======
- 异常返回结果
====== 1 Around start ======
====== 2 doBefore ======
====== 3-2 doAfterThrowing ======
====== 4 doAfter ======