你可以这样做:
注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Profiled {
public boolean showArguments();
}
拦截器:
@Aspect
public class ProfilingAspect {
public static Logger log = LoggerFactory.getLogger("ProfilingAspect");
@Around("@annotation(profiled)")
public Object profiled(final ProceedingJoinPoint pjp,
final Profiled profiled) throws Throwable {
StopWatch sw = new StopWatch();
try {
sw.start();
return pjp.proceed();
} finally {
sw.stop();
StringBuilder sb = new StringBuilder();
sb.append("Method ");
sb.append(pjp.getSignature().getName());
if (profiled.showArguments()) {
sb.append(" with arguments ");
sb.append(Arrays.toString(pjp.getArgs()));
}
sb.append(" took ");
sb.append(sw.getTime());
sb.append(" millis");
log.info(sb.toString());
}
}
}