簡體   English   中英

Spring-AOP:@AfterReturning 立即執行

[英]Spring-AOP:@AfterReturning is executed immediately

嗨,我正在使用 Spring AOP 進行日志記錄,我有以下兩個方面 @Before 和 @AfterReturning 不幸的是,兩者都在這里打印相同的響應,期望是 @Before 打印方法輸入和 @AfterReturning 打印方法輸出。

 @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))
    }

如果您告訴它們,這兩種建議方法打印出相同的內容也就不足為奇了。 在這兩種情況下,您都說要打印方法參數 您無處可說應該打印結果 您可以通過@AfterReturning注釋中的可選returning參數來完成此操作。

也許你想查看Spring 手冊,有一個你想要做什么的例子(甚至在 Kotlin 中可用!)。

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

當然,如果您的建議中同時需要連接點和返回值,您也可以將兩者都綁定到方法參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM