定义
后置异常通知
在连接点执行失败之后执行通知,使用@AfterThrowing指定切入点和通知方法。
参数
JoinPoint
在需要连接点(异常)的信息时,可以为通知添加一个JoinPoint类型的参数来获取。
参数位置
在Spring中,建议将
JoinPoint作为第一个参数。
异常
在需要异常时,为通知添加一个异常类型的参数并将@AfterThrowing的throwing赋值为参数名。
示例
```java
@Aspect
@Component
@Slf4j
public class LogAspect {
@PointCut("@annotation(com.example.annotation.Log)")
public void logAnnotation() { }
@AfterThrowing(pointcut = "logAnnotation()", throwing = "exception")
public void logAfterThrowing(JoinPoint joinPoint,Throwable exception) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
log.info(“after method:{} failed:{}”, method.getName(), exception.getMessage());
}
}