Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析
HandlerInterceptor 在這里看到這個(gè)HandlerExecutionChain對(duì)interceptor的調(diào)用,在這里深入看一下。
HandlerExecutionChain 就是一個(gè)類,綁定了Handler( 對(duì)應(yīng)的Controller) 和 Interceptors , 所以作用就是對(duì)Controller前后執(zhí)行interceptors, 類似Filter
幾個(gè)問(wèn)題:
1. interceptor調(diào)用位置?
1 2 3 對(duì)應(yīng)調(diào)用 handlerInterceptor preHandle, postHandle, afterCompletion
先看第一行的:
進(jìn)到對(duì)應(yīng)的interceptor實(shí)現(xiàn)類,preHandler返回true, 如果返回false, 就會(huì)到DispatcherServlet就直接return了。
看第二行沒(méi)有特別的:
由于它只重寫了preHandle
所以走到它父類的postHandle了。方法是空的。
第三行和第二行一樣不贅述了。
以上就是執(zhí)行位置。
所以如果實(shí)現(xiàn)HandlerInterceptor , preHandle 應(yīng)該返回true, 具體要攔截的內(nèi)容寫在return true之前就行。 postHandle和afterCompletion 都是void方法,直接在里面寫需要攔截的內(nèi)容。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題2. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)3. vue實(shí)現(xiàn)web在線聊天功能4. Springboot 全局日期格式化處理的實(shí)現(xiàn)5. python 浮點(diǎn)數(shù)四舍五入需要注意的地方6. idea配置jdk的操作方法7. Docker容器如何更新打包并上傳到阿里云8. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法9. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法10. JAMon(Java Application Monitor)備忘記
