I have a spring application and I want to use aspects to perform some performance logging.
I want to only log methods annotated with @Measured, so I created an annotation as follows :
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Measured {
boolean logParameters() default true;
boolean logResponse() default true;
boolean measureTime() default true;
}
The aspect is as follows:
@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);
}
}
}
This works just fine for a method like
@Measured
public boolean getUser() {
}
Now I want to annotate a spring mvc controller method that is already annotated with @RequestMapping, but the apsect doesn't seem to detect it.
@Measured
@RequestMapping(value = "/activity", method = GET)
public final String userProfile(@ModelAttribute(USER) User user,
Model model) {
//Do Something
}
How can i make the aspect detect this method? I am using spring aop definded in the context xml file as <aop:aspectj-autoproxy proxy-target-class="false"/>