我撰寫了一個簡單的 AOP 來記錄請求和執行時間。一切正常,但是在使用注釋記錄執行時間時,盡管 http 狀態代碼為 200,但它并沒有重新調整任何回應。
你能告訴我是什么問題嗎?
控制器:
@LogExecutionTime
@GetMapping("/test")
public String index(){
return "Hello";
}
方面:
@Around("@annotation(LogExecutionTime)")
public void logTime(ProceedingJoinPoint jp) throws Throwable{
watch.start() // common lang stopwatch
jp.proceed();
watch.stop();
log,info("Execution time " watch);
}
在日志中我可以看到它顯示了執行時間,但是在郵遞員中,如果我評論 @LogExecutionTime 注釋它作業正常,我 沒有得到回應“你好”
uj5u.com熱心網友回復:
“around”型別的切面可以修改回傳值。該proceed方法從原始方法回傳值,您會默默地忽略它,并選擇不回傳任何內容(我相信這會默認呼叫回傳 null)。您需要按如下方式修改您的方法:
@Around("@annotation(LogExecutionTime)")
public Object logTime(ProceedingJoinPoint jp) throws Throwable{
watch.start(); // common lang stopwatch
Object returnValue = jp.proceed();
watch.stop();
log.info("Execution time " watch);
return returnValue;
}
您可能還希望將您的watch.stop()呼叫放在一個finally塊中,以防觀察到的方法引發例外。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/475953.html
標籤:爪哇 春天 弹簧靴 spring-aop
