AOP执行顺序

    技术2022-07-10  109

    package com.zzhua.aspect; import com.alibaba.fastjson.JSON; import com.zzhua.enums.StatusEnums; import com.zzhua.pojo.Log; import com.zzhua.service.LogService; import com.zzhua.utils.StringUtils; import com.zzhua.utils.ThreadLocalContext; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Date; @Aspect @Component @Slf4j public class RequestAspect { @Autowired private LogService logService; /** * 正常执行顺序: 请求先到达 -> 环绕通知,调用连接点时 -> 进入前置通知 -> 执行controller方法 * -> 回到环绕通知连接点完毕处 -> 后置通知 -> 返回通知 * * * 异常执行顺序: 请求先到达 -> 环绕通知,调用连接点时 -> 进入前置通知 -> 执行controller方法(发生异常) * -> 后置通知 -> 异常通知 -> 全局异常处理器 * */ @Pointcut("execution(* com.zzhua.controller..*(..))") public void pointCut() { } /** * 请求进入controller方法前拦截 * @param joinPoint */ @Before("pointCut()") public void doBefore(JoinPoint joinPoint) { // Tomcat在处理请求时,封装request对象,并将此对象放入ThreadLocal变量中(键为当前线程对象) ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); log.info("请求地址: {}", request.getRequestURI()); log.info("请求方式: {}", request.getMethod()); // 获取真实ip log.info("IP: {}" , StringUtils.getRemoteIp(request)); // 访问方法 log.info("方法: {}" , joinPoint.getSignature()); // 访问参数 log.info("参数: {}" , Arrays.toString(joinPoint.getArgs())); Log logger = ThreadLocalContext.get().getLog(); logger.setLogUrl(request.getRequestURI()); logger.setLogParams( Arrays.toString(joinPoint.getArgs())); logger.setLogStatus(StatusEnums.REQUEST_SUCCESS.getCode()); logger.setLogMethod(request.getMethod()); logger.setLogIp(StringUtils.getRemoteIp(request)); } @After("pointCut()") public void doAfter(){ // System.out.println("doAfter..."); } /** * 环绕通知,统计请求花费时间 * @param joinPoint * @return * @throws Throwable */ @Around("pointCut()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { long beginTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); long duration = endTime - beginTime; log.info("耗时: {}" , duration); ThreadLocalContext.get().getLog().setLogTime(duration); return result; } /** * 返回通知 (如果发生异常,不会走此通知,后置通知会走) * @param ret */ @AfterReturning(value = "pointCut()",returning = "ret") public void doAfterReturning(Object ret){ log.info("返回: {}",JSON.toJSONString(ret) ); Log logger = ThreadLocalContext.get().getLog(); logger.setLogResult(JSON.toJSONString(ret)); logger.setCreatedTime(new Date()); logService.insert(logger); } /** * 异常通知 * @param ex */ @AfterThrowing(value = "pointCut()", throwing = "ex") public void doAfterThrowing(Exception ex){ Log logger = ThreadLocalContext.get().getLog(); logger.setLogStatus(StatusEnums.REQUEST_ERROR.getCode()); logger.setLogMessage(StringUtils.getPackageException(ex,"com.zzhua")); logger.setLogTime(0L); logger.setCreatedTime(new Date()); logService.insert(logger); } }
    Processed: 0.012, SQL: 9