我有一个 spring 应用程序,我想使用方面来执行一些性能日志记录。
我只想记录使用@Measured 注释的方法,所以我创建了如下注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Measured {
boolean logParameters() default true;
boolean logResponse() default true;
boolean measureTime() default true;
}
方面如下:
@Aspect
@Component
public class MeasuredAspect {
@Pointcut(value = "@annotation(com.waelawada.web.Measured)")
public void isMeasurableEvent(){
}
@Around(value = "isMeasurableEvent() && @annotation(measured)")
public void addEventToMDC(ProceedingJoinPoint joinPoint, Measured measured) throws Throwable{
String methodName = joinPoint.getSignature().getName();
long startTime = System.currentTimeMillis();
joinPoint.proceed(joinPoint.getArgs());
long endTime = System.currentTimeMillis();
if(measured.measureTime()){
MDC.put(methodName,endTime-startTime);
}
}
}
这适用于像这样的方法
@Measured
public boolean getUser() {
}
现在我想注释一个已经用@RequestMapping注释的spring mvc控制器方法,但是apsect似乎没有检测到它。
@Measured
@RequestMapping(value = "/activity", method = GET)
public final String userProfile(@ModelAttribute(USER) User user,
Model model) {
//Do Something
}
我怎样才能让方面检测到这种方法?我正在使用上下文 xml 文件中定义的 spring aop 作为<aop:aspectj-autoproxy proxy-target-class="false"/>