Spring Boot Aspect @Around

旧巷老猫 提交于 2019-12-25 09:39:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!