0

我想以不同的方式记录 Controller 和其他包。我知道我可以为此使用 2 种单独的方法,但是这 2 种方法非常相似,所以我想添加一个代码来检查它看起来像这样

@Around("controllerPoint() || theRest()")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
  if( called from controllerPoint() ) {
      execute this short section of code                     # (1)            
  }
// rest of code

这段代码会是什么样子?

另外,如果在执行 (1) 之后,我想在为其他包执行时再次将变量传递给相同的方法,我该怎么做?

4

2 回答 2

0

您可以调用如下方法,该方法将返回您的方法名称

joinPoint.getSignature().getName()
于 2019-09-25T07:56:37.433 回答
0

您可以从连接点获取方法名称:

@Aspect
@Configuration
public class TrackingConfig {

    @Around("execution(* your.package.Controller.*(..))")
    public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        if ("theRest".equals(methodName)) {
            System.out.println("AROUND! theRest ");
        } else if ("controllerPoint".equals(methodName)) {
            System.out.println("AROUND! controllerPoint ");
        }
        return pjp.proceed();
    }
}
于 2019-09-25T07:59:29.923 回答