Spring MVC中自定義攔截器的實(shí)例講解
1. 引言
攔截器(Interceptor)實(shí)現(xiàn)對每一個(gè)請求處理前后進(jìn)行相關(guān)的業(yè)務(wù)處理,類似于Servlet的Filter。
我們可以讓普通的Bean實(shí)現(xiàn)HandlerIntercpetor接口或繼承HandlerInterceptorAdapter類來實(shí)現(xiàn)自定義攔截器。
通過重寫WebMvcConfigurerAdapter的addIntercetors方法來注冊一個(gè)計(jì)算每一次請求的處理時(shí)間的攔截器。
2. 自定義攔截器的實(shí)現(xiàn)
2.1 定義攔截器
新建LogInterceptor類,并繼承HandlerInterceptorAdapter類,重寫preHandle、postHandle這兩個(gè)方法。
1.preHandle方法表示在請求發(fā)生前執(zhí)行,內(nèi)容如下:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("begin", System.currentTimeMillis());
return true;
}
2.postHandle方法表示在請求完成后執(zhí)行,內(nèi)容如下:
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
long begin = (long)request.getAttribute("begin");
request.removeAttribute("begin");
long end = System.currentTimeMillis();
System.out.println("本次請求消耗時(shí)間為:"+new Long(end-begin)+"ms");
}
2.2 配置攔截器
2.2.1 使用xml配置
1.在配置文件中添加支持MVC的schema
xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
2.使用mvc:interceptors標(biāo)簽聲明攔截器
<mvc:interceptors>
<!-- 使用bean定義一個(gè)Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截所有的請求 -->
<bean class="org.aming.demo.springmvc.interceptor.LogInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="${指定的URL}"/>
<!-- 定義在mvc:interceptor下面的表示是對特定的請求才進(jìn)行攔截的 -->
<bean class="${其他攔截器}"/>
</mvc:interceptor>
</mvc:interceptors>
說明:沒有測試過?。。?/span>
2.2.2 使用JavaConfig配置
3.配置攔截器的Bean
@Bean
public LogInterceptor logInterceptor() {
return new LogInterceptor();
}
4.重寫addInterceptors方法,注冊攔截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor());
}
說明:配置類需要繼承WebMvcConfigurerAdapter類
3. 運(yùn)行結(jié)果

以上這篇Spring MVC中自定義攔截器的實(shí)例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Gateway全局異常處理的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Cloud Gateway全局異常處理的相關(guān)資料,需要的朋友可以參考下2018-10-10
spring boot 下對JSON返回值去除null和空字段操作
這篇文章主要介紹了spring boot 下對JSON返回值去除null和空字段操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
詳解springboot通過Async注解實(shí)現(xiàn)異步任務(wù)及回調(diào)的方法
這篇文章主要介紹了springboot通過Async注解實(shí)現(xiàn)異步任務(wù)及回調(diào),文中通過一個(gè)簡單示例來直觀的理解什么是同步調(diào)用,在單元測試用例中,注入?SyncTask?對象,并在測試用例中執(zhí)行?doTaskOne(),doTaskTwo(),doTaskThree()?三個(gè)方法,具體實(shí)現(xiàn)方式跟隨小編一起看看吧2022-05-05
使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出
這篇文章主要介紹了使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出,EasyExcel是一個(gè)快速解決大文件內(nèi)存溢出的Excel處理工具,它能讓你在不用考慮性能、內(nèi)存等因素的情況下,快速完成Excel的讀、寫等功能,需要的朋友可以參考下2023-10-10
Mybatis新增數(shù)據(jù)并返回主鍵id的兩種方法實(shí)現(xiàn)
本文主要介紹了Mybatis新增數(shù)據(jù)并返回主鍵id的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
PageHelper插件實(shí)現(xiàn)一對多查詢時(shí)的分頁問題
這篇文章主要介紹了PageHelper插件實(shí)現(xiàn)一對多查詢時(shí)的分頁問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

