简体   繁体   中英

How to use spring aop to log cost time of method annotated with @RequestMapping?

I'd like to log cost time of all method annotated with @RequestMapping. However below code doesn't work.

@Component
@Aspect
@Slf4j
public class LogAop {
    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void req() {}

    @Before("req()")
    public void logMethod(JoinPoint jp) {
        String methodName = jp.getSignature().getName();
    }


 @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object doAroundController(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.currentTimeMillis();
        log.info(" method {} begin",
                pjp.getSignature().getName());

        Object o;

        try {
            o = pjp.proceed();
        } catch (Throwable e) {
            throw e;
        } finally {
            long costTime = System.currentTimeMillis() - begin;
            log.info(" method {} ended  cost time {}ms",
                    pjp.getSignature().getName(), costTime);
        }

        return o;
    }

}

Both doAroundController and logMethods don't work.

If I change code above to code below, that will work:

@Pointcut("execution(public * *(..))")
public void publicMethod() {}




@Around("publicMethod()")
public Object doAroundController(ProceedingJoinPoint pjp) throws Throwable {

In applicationContext.xml, I enable spring aop using this line:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-autowire="byType" default-lazy-init="true">
...
    <aop:aspectj-autoproxy/>

Using JDK 7, Spring MVC 2.5.6, AspectJ 1.7.2.

I have something like that and it works (most important change: within instead of annotation ). I'm not sure now, but I think I wanted to achive the same functionality and couldn't do it that way, so I added aspect on every method in the controller, since they all are annotated with @RequestMapping anyway (or at least should be).

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
void restController() {}

@Pointcut("execution(public * *(..))")
void publicMethod() {}

@Before("restController() && publicMethod()")
void myMethod(JoinPoint joinPoint) {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM