弹簧启动方面 @Around.



我正在使用Spring Boot并尝试记录每个请求的响应时间。为此,我试图@Around方面。法典:

@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应该被打印出来,但他们没有。我错过了什么?

这个

怎么样

@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();
   ...............
}

最新更新