0

Hi I am using Spring AOP for logging ,I have two following aspects @Before and @AfterReturning Unfortunately both are printing the same response here the expectation is @Before print method input and @AfterReturning print method output.

 @Before(value ="execution(* com.abc.xyz.service..*(..))")
    fun logBeforeAllMethods(joinPoint: JoinPoint) {
        log.info("Service Request : " + Arrays.toString(joinPoint.args))
    }

    @AfterReturning(value="execution(* com.abc.xyz.service..*(..))")
    fun logAfterAllMethods(joinPoint: JoinPoint) {
        log.info("Service Response : " + Arrays.toString(joinPoint.args))
    }
Rocky4Ever
  • 828
  • 3
  • 12
  • 35

1 Answers1

1

It is no surprise that both advice methods print the same if you tell them to. In both cases you say you want to print the method arguments. Nowhere are you stating that the result ought to be printed. You do that via the optional returning parameter in the @AfterReturning annotation.

Maybe you want to check the Spring manual, there is an example for what you want to do (even available in Kotlin!).

@Aspect
class AfterReturningExample {
  @AfterReturning(
    pointcut = "com.xyz.myapp.CommonPointcuts.dataAccessOperation()",
    returning = "retVal")
  fun doAccessCheck(retVal: Any) {
      // ...
  }
}

Of course you can also bind both the joinpoint and return value to method parameters if you need both in your advice.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • For some reason this is working by on other methods other than controller methods – Rocky4Ever Oct 01 '20 at 14:36
  • I do not understand your comment. What exactly is working and what is not? – kriegaex Oct 01 '20 at 16:45
  • Sorry some of my methods have return type of Mono it it hard to log Mono object – Rocky4Ever Oct 01 '20 at 18:45
  • That is a completely new question and I can see that you already posted a [related question](https://stackoverflow.com/q/64164101/1082681) separately. I answered this question correctly, so before asking follow-up questions, please be so kind as to accept and upvote this one first. – kriegaex Oct 02 '20 at 00:26