[英]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. 我想记录所有用@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. doAroundController
和logMethods
都不起作用。
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: 在applicationContext.xml中,我使用以下代码启用spring aop:
<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. 使用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
). 我有类似的东西,它可以工作(最重要的变化:
within
而不是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). 我现在不确定,但是我想我想实现相同的功能并且不能那样做,所以我在控制器的每个方法上都添加了
@RequestMapping
,因为无论如何它们都被@RequestMapping
注释(至少应该)。
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
void restController() {}
@Pointcut("execution(public * *(..))")
void publicMethod() {}
@Before("restController() && publicMethod()")
void myMethod(JoinPoint joinPoint) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.