连接点:@Log的方法注解
通知:

  1. /**
  2. * 配置异常通知
  3. * # 切入点抛出异常后调用该方法
  4. *
  5. * @param joinPoint join point for advice
  6. * @param e exception
  7. */
  8. @AfterThrowing(pointcut = "logPointcut()", throwing = "e")
  9. public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
  10. Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
  11. currentTime.remove();
  12. log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
  13. HttpServletRequest request = RequestHolder.getHttpServletRequest();
  14. logService.save(getUsername(),
  15. StringUtils.getIp(RequestHolder.getHttpServletRequest()),
  16. (ProceedingJoinPoint) joinPoint, log, getUid());
  17. }

异常:springboot的@Validated检测异常,没有触发afterThrowing的通知。

  1. 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标注的方法抛出的异常,因此不能触发该切面的异常通知。