Spring Boot Aspect @Around [英] Spring Boot Aspect @Around

查看:137
本文介绍了Spring Boot Aspect @Around的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");
        }

}

代码被编译,但是问题是当我执行任何控制器方法时,什么都没有发生.我的意思是我的SOP应该打印,但不是. 我想念什么?

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?

推荐答案

如何解决

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

这篇关于Spring Boot Aspect @Around的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆