【发布时间】:2018-11-25 09:21:15
【问题描述】:
我有一个适用于公共方法的 AspectJ 编织注释,但私有方法被忽略了。 此方法的目的是简单地记录运行该函数所花费的时间。
@Aspect
@Slf4j
public class TimedLogAspect {
@Pointcut("@annotation(timedLogVar)")
public void annotationPointCutDefinition(TimedLog timedLogVar) {}
@Pointcut("execution(* *(..))")
public void atExecution() {}
@Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
Stopwatch stopwatch = Stopwatch.createStarted();
Object returnValue = joinPoint.proceed();
stopwatch.stop();
MessageBuilder messageBuilder = new MessageBuilder(joinPoint.toShortString(), stopwatch.elapsed(TimeUnit.MILLISECONDS))
.attachMessage(timedLogVar.message())
.attachMethodArgs(timedLogVar.shouldAttachMethodArgs(), Stream.of(joinPoint.getArgs()).collect(Collectors.toList()))
.attachReturnValue(timedLogVar.shouldAttachReturnValue(), returnValue);
log.info(messageBuilder.build(), messageBuilder.getArgs().toArray());
return returnValue;
}
}
这是实际的界面:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimedLog {
boolean shouldAttachMethodArgs() default false;
boolean shouldAttachReturnValue() default false;
String message() default "";
}
我看到了很多答案,在execution 部分的第一个* 之前添加private,我看到privileged 不支持注释,我正在使用没有 SpringAOP 的 AspectJ。
有什么想法吗?
【问题讨论】: