攔截器 Interceptor

是 AOP 的一種實作,基於 Java 反射與動態代理機制實現。

如何實作

  1. 實作 HandlerInterceptor 介面,實現 preHandlepostHandleafterCompletion 方法。

  2. 通過配置加入攔截器,實現 WebMvcConfigurer 介面,覆寫 addInterceptors 方法。

Spring 中的攔截流程

ServletContextListener > Filter >

Interceptor > AOP > 具體執行的方法 > AOP > @ControllerAdvice > Interceptor

> Filter > ServletContextListener

參考資料

https://www.chainnews.com/zh-hant/articles/444319414168.htm

Last updated

Was this helpful?