这是我第一次接触AOP。我有一个带有一个Aspect(一个记录器)的spring-boot应用程序。搜索我得出的结论是@Around方法在该方法之前和之后执行(我只是在一个方法中调用它),对吗?在我的@Around方法中间,y为joinPoint.proceed()
。如果我没看错,JoinPoint
是我必须用来获取要调用方面的方法的信息的对象,但我无法理解该过程实际在做什么!
这是我的代码:
@Around(value = "execution(* *(..)) && @annotation(Loggable)", argNames = "ProceedingJoinPoint, Loggable")
public Object logAround(ProceedingJoinPoint joinPoint, Loggable loggable) throws Throwable {
String methodArguments = loggable.printMethodArguments() ? Arrays.toString(joinPoint.getArgs()) : "[]";
long start = System.currentTimeMillis();
Object returnObject = joinPoint.proceed(); // continue on the
// intercepted method
long elapsedTime = System.currentTimeMillis() - start;
String returnValue = loggable.printReturn() && returnObject != null ? returnObject.toString() : "[]";
LOG.info("Logging method: {}.{} Method arguments: {}. Method return value: {}. Method execution time: {}",
joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName(), methodArguments,
returnValue, elapsedTime);
return returnObject;
}
正如您所提到的,@Around
建议围绕一个连接点,例如方法调用。它可以在方法调用之前和之后执行自定义行为。它还负责选择是继续进行连接点还是捷径建议的方法执行。在您的情况下,实际的方法调用(那些包含非常有用的业务逻辑的方法!)的发生是由于joinPoint.proceed();
。