spring aop统计请求执行时间

感觉获取request有点麻烦了,以下是代码:

package com.caiya.software.web.interceptors;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by caiya on 15/9/28.
 */
@Aspect
@Component
public class LogInterceptor {


    private static final Logger logger = Logger.getLogger(LogInterceptor.class);


    @Before("pointcut()")
    public void before() throws IllegalAccessException{

    }

    @Pointcut("execution(* com.caiya.software.web.controllers.*.*(..))")
    public void pointcut(){

    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{

        long startTime = System.currentTimeMillis();
        Object retVal = pjp.proceed();
        long endTime = System.currentTimeMillis();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String requestURI = request.getRequestURI();
        logger.debug("request uri : " + requestURI + " && request args : "+ pjp.getArgs() + " && took : " + (endTime-startTime) + "ms");

        String className =pjp.getSignature().getDeclaringTypeName();
        String method = pjp.getSignature().getName();
        logger.debug("当前线程: "+Thread.currentThread()+" ========类名:" + className+" ========方法名:" + method + "=======执行时间:" + (endTime-startTime) + "毫秒");

        return retVal;
    }


    @After("pointcut()")
    public void after(){

    }

    

}


你可能感兴趣的:(spring aop统计请求执行时间)