AOP之Advice类型
- MethodBeforeAdvice,在方法执行之前的切面逻辑
/**
* @author 周瑜
*/
public class ZhouyuBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法执行前执行");
}
}
- AfterReturningAdvice,方法返回后执行的切面逻辑
/**
* @author 周瑜
*/
public class ZhouyuAfterReturningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法return后执行");
}
}
- ThrowsAdvice,方法抛异常后执行的切面逻辑
/**
* @author 周瑜
*/
public class ZhouyuThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, NullPointerException ex) {
System.out.println("方法抛出异常后执行");
}
}
- MethodInterceptor,任意控制
/**
* @author 周瑜
*/
public class ZhouyuAroundAdvice implements MethodInterceptor {
@Nullable
@Override
public Object invoke(@NotNull MethodInvocation invocation) throws Throwable {
System.out.println("方法执行Around前");
Object proceed = invocation.proceed();
System.out.println("方法执行Around后");
return proceed;
}
}
![](/images/780-200-2.jpg)