连接点:@Log的方法注解
通知:
/**
* 配置异常通知
* # 切入点抛出异常后调用该方法
*
* @param joinPoint join point for advice
* @param e exception
*/
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
currentTime.remove();
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
HttpServletRequest request = RequestHolder.getHttpServletRequest();
logService.save(getUsername(),
StringUtils.getIp(RequestHolder.getHttpServletRequest()),
(ProceedingJoinPoint) joinPoint, log, getUid());
}
异常:springboot的@Validated检测异常,没有触发afterThrowing的通知。
org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<java.lang.Object> co.yixiang.lujia.village.rest.CellController.create(co.yixiang.lujia.village.domain.Cell) with 2 errors: [Field error in object 'cell' on field 'pid': rejected value [null]; codes [NotBlank.cell.pid,NotBlank.pid,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [cell.pid,pid]; arguments []; default message [pid]]; default message [不能为空]] [Field error in object 'cell' on field 'townName': rejected value [null]; codes [NotBlank.cell.townName,NotBlank.townName,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [cell.townName,townName]; arguments []; default message [townName]]; default message [不能为空]]
解决
spring对参数校验的检测也是通过切面的形式。@Validated是在通知中抛出异常的,不属于@Log标注的方法抛出的异常,因此不能触发该切面的异常通知。