Spring Boot Aspect @Around

时间:2017-05-16 11:38:48

标签: spring-boot aspectj

我正在使用Spring Boot&尝试记录每个请求的响应时间。 为此,我正在尝试@Around Aspect。 代码:

@Aspect
@Component
public class XYZ {

       @Around("execution(* org.springframework.web.servlet.DispatcherServlet.service(..))")
        public void doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
            // start stopwatch
            long startTime = System.currentTimeMillis();
            System.out.println("Before");
            pjp.proceed();
            long endTime = System.currentTimeMillis();
            // stop stopwatch
            System.out.println("Me here");
        }

}

代码被编译,但问题是当我执行任何控制器方法时,没有任何反应。我的意思是我的SOP应该打印但是它们不是。 我错过了什么?

1 个答案:

答案 0 :(得分:0)

这个怎么样

@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void requestMapping() {}

@Pointcut("within(path.to your.controller.package.*)")
public void myController() {}

@Around("requestMapping() || myController()")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
   ...............
   joinPoint.proceed();
   ...............
}
相关问题