詳解SpringMVC的攔截器鏈實現(xiàn)及攔截器鏈配置
1、攔截器鏈實現(xiàn)
只需定義多個攔截器的 bean,然后在攔截器的配置類中將其逐一添加即可
第一個攔截器的定義:
@Component public class ProjectInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle111..."); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle111..."); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion111..."); } }
第二個攔截器的定義:
@Component public class ProjectInterceptor2 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle222..."); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle222..."); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion222..."); } }
攔截器配置類:
@Configuration public class SpringMvcSupport extends WebMvcConfigurationSupport { @Autowired private ProjectInterceptor1 interceptor1; @Autowired private ProjectInterceptor2 interceptor2; @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(interceptor1).addPathPatterns("/books", "/books/*"); registry.addInterceptor(interceptor2).addPathPatterns("/books", "/books/*"); } }
2、攔截器鏈的執(zhí)行順序
攔截器配置類中,首先被添加的攔截器在外層,之后添加的在內(nèi)層,請求發(fā)出之后首先被外層的攔截器攔截,外層放行后進入內(nèi)層,而離開攔截器時先離開內(nèi)層的攔截器,再離開外層。
上例子中第一個攔截器在外層,第二個在內(nèi)層,所以上例中攔截器的輸出順序為:
preHandle111...
preHandle222...
postHandle222...
postHandle111...
afterCompletion222...
afterCompletion111...
值得注意的是,afterCompletion 方法的一定會在 postHandle 之后執(zhí)行,并且也是由內(nèi)層向外層執(zhí)行,所以 afterCompletion222 輸出在 postHandle111 之后,而在 afterCompletion111 之前。換言之,即便有再多的攔截器,最先執(zhí)行的 afterCompletion 也一定在最后執(zhí)行的 postHandle 之后才執(zhí)行
此外,當攔截器的 preHandle 方法返回 false 時,其內(nèi)層的攔截器以及它自身 postHandle 和 afterCompletion 都不會再執(zhí)行,而所有外層攔截器的 postHandle 不再執(zhí)行,但 afterCompletion 照常執(zhí)行
這是因為 postHandle 執(zhí)行在原始 Controller 方法執(zhí)行之后,必須所有攔截器都放行時,才會執(zhí)行原始方法,也就才會執(zhí)行 postHandle 方法
上例中第二個攔截器 preHandle 返回 false 時的輸出順序:
preHandle111...
preHandle222...
afterCompletion111...
到此這篇關(guān)于詳解SpringMVC的攔截器鏈實現(xiàn)及攔截器鏈配置的文章就介紹到這了,更多相關(guān)SpringMVC攔截器鏈 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java并發(fā)編程示例(二):獲取和設(shè)置線程信息
這篇文章主要介紹了Java并發(fā)編程示例(二):獲取和設(shè)置線程信息,本文是系列文章的第二篇,本文著重講解Thread類的幾個重要屬性,需要的朋友可以參考下2014-12-12使用@Autowired注解引入server服務(wù)層方法時報錯的解決
這篇文章主要介紹了使用@Autowired注解引入server服務(wù)層方法時報錯的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11IDEA自動生成TestNG的testng.xml的插件方法
這篇文章主要介紹了IDEA自動生成TestNG的testng.xml的插件方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04SpringBoot?把PageHelper分頁信息返回給前端的方法步驟
本文主要介紹了SpringBoot?把PageHelper分頁信息返回給前端的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01Java?基于Hutool實現(xiàn)DES加解密示例詳解
這篇文章主要介紹了Java基于Hutool實現(xiàn)DES加解密,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08基于Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié)
這篇文章主要介紹了Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08IntelliJ IDEA中查看當前類的所有繼承關(guān)系圖
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中查看當前類的所有繼承關(guān)系圖,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10