参考: https://www.cnblogs.com/Geyoung/p/6927905.html

 

 

@Aspect
@Component
public class TimeAspect {
    //通过这种方法可以拿到对象中所有的方法,对象
    @Around("execution(* com.imooc.web.controller.UserController.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        
        System.out.println("time aspect start");
        
        Object[] args = pjp.getArgs();
        for (Object arg : args) {  
            System.out.println("arg is "+arg);
        }
        
        long start = new Date().getTime();
        
        Object object = pjp.proceed();
        
        System.out.println("time aspect 耗时:"+ (new Date().getTime() - start));
        
        System.out.println("time aspect end");
        
        return object;
    }

}
View Code

 

@Aspect
@Component
public class TimeAspect {

    private  static Logger  logger =LoggerFactory.getLogger(TimeAspect.class);
    @Around("execution(* com.xxxx.payment.controller.*.*(..))")
    public Object handleAroundControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        String className = pjp.getTarget().getClass().getSimpleName();
        String methodName = pjp.getSignature().getName();
        logger.info("start  run  {} method",methodName);
        Object[] args = pjp.getArgs();
        logger.info("enter  {}.{} receive data with : {}", className,methodName,args);
        Object object = pjp.proceed();
        String costTime = (System.currentTimeMillis() - start) + "";
        logger.info("***************end the method --> {} total cost time is {} ms ********************", methodName, costTime);

        return object;
    }
}

 

posted on 2018-04-01 21:41  lshan  阅读(127)  评论(0编辑  收藏  举报