问题
I am using Spring Boot & try to log response time of every request. For that purpose, I am trying to @Around Aspect. Code :
@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");
}
}
The code gets compiled, but the issue is that when I am executing any controller method, nothing happens. I mean my SOP should get printed but they aren't. What am i missing?
回答1:
How about this
@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();
...............
}
来源:https://stackoverflow.com/questions/44000449/spring-boot-aspect-around