关于java:SpringAOP-环绕通知避坑

49次阅读

共计 937 个字符,预计需要花费 3 分钟才能阅读完成。

盘绕告诉必须有返回值,返回值即为指标办法的返回值,否则指标办法会无奈完结,导致前端申请认为超时!
return object;
必须有,别问,问就是被坑过

@Aspect
@Component
public class WebLogAspect {private Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution(public * com.example.liaowh.controller..*.*(..))")
    public void webLog() {}

    @Around("webLog()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        // 接管到申请,记录申请内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 记录下申请内容
        logger.info("URL :" + request.getRequestURL().toString());
        logger.info("HTTP_METHOD :" + request.getMethod());
        logger.info("IP :" + request.getRemoteAddr());
        logger.info("CLASS_METHOD :" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        logger.info("ARGS :" + Arrays.toString(joinPoint.getArgs()));
        Object object = joinPoint.proceed(joinPoint.getArgs());
        logger.info("return" + object);
        return object;
    }
}

正文完
 0