0

I want to log Controller and the rest of packages differently. I know I can use 2 separate methods for this but these 2 methods are very similar, so I want to add a code to check that would look something like this

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

What would this code be like?

Also, if after I execute (1) and I want to pass a variable to this same method again when it executes for other packages, how can I do it?

Muse
  • 13
  • 2
  • Don't... Just write the 2 methods and extract the shared stuff into a method that you call from both. That will be much easier to reason about that trying to shoehorn all of it into a single method. – M. Deinum Sep 25 '19 at 07:41

2 Answers2

0

you can invoke the method like the below which will return your method name

joinPoint.getSignature().getName()
clevertension
  • 6,929
  • 3
  • 28
  • 33
0

You could get method name, from join point:

@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();
    }
}
i.bondarenko
  • 3,442
  • 3
  • 11
  • 21