@AfterReturning

Reference: Spring Docs - @AfterReturning

After returning advice ๋ž€?

  • ์ง€์ •ํ•œ method์˜ ์‹คํ–‰์ด ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๊ณ  return ํ•œ ํ›„ ์‹คํ–‰๋œ๋‹ค

    • target method๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€์ง€ ์•Š๊ณ  ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋œ ๊ฒฝ์šฐ์—๋งŒ ์‹คํ–‰๋œ๋‹ค

  • ์ฃผ๋กœ target method์˜ return ๊ฐ’์„ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜, logging๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค

Options

  • pointcut

    • ์–ด๋–ค method์— ๋Œ€ํ•ด advice๋ฅผ ์ ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•œ๋‹ค

      • AspectJ์˜ pointcut ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ method๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค

    • ex)

      @AfterReturning(pointcut = "execution(* com.example.service.MyService.*(..))")
      public void afterReturningAdvice() {
          // advice ๋‚ด์šฉ
      }
  • returning

    • target method์˜ return ๊ฐ’์„ ๋ฐ›์„ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค

      • advice ๋‚ด์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ target method return ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค

    • ex)

      @AfterReturning(pointcut = "execution(* com.example.service.MyService.*(..))", returning = "result")
      public void afterReturningAdvice(Object result) {
          // result ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋Œ€์ƒ method์˜ return ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
      }
  • argNames

    • pointcut์—์„œ ์ง€์ •ํ•œ method์˜ argument ์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ advice ๋‚ด์—์„œ argument์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค

      • argument ์ด๋ฆ„์„ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

    • ex)

      @AfterReturning(pointcut = "execution(* com.example.service.MyService.someMethod(..))", returning = "result", argNames = "param1,param2")
      public void afterReturningAdvice(Object result, String param1, int param2) {
          // result, param1, param2 ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด return ๊ฐ’๊ณผ parameter ์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
      }

Last updated