SpringMVC中的攔截器不生效的問題解決
過濾器代碼(被Spring掃描并管理):
@Component
public class StuInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("前置過濾器");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("后置過濾器");
}
}
過濾器配置代碼:
@Configuration
public class MvcSupport implements WebMvcConfigurer {
@Resource
private StuInterceptor stuInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
System.out.println(stuInterceptor);
registry.addInterceptor(stuInterceptor).addPathPatterns("/stu");
}
}
按理說我們發(fā)出請求localhost/stu
之后,應(yīng)該可以看到過濾器的效果,但是失效了。
網(wǎng)上的說法眾說紛紜:
- 沒加@Component或者@Configuration注解
- @ComponentScan沒掃描到
- 路徑配置錯了
以上三種說法一一排除之后,我發(fā)現(xiàn)一個博客提到了:
我想到在SpringMVC的配置類上使用了@EnableWebMvc注解,而這個注解相當(dāng)于在容器中引入了DelegatingWebMvcConfiguration類,而這個DelegatingWebMvcConfiguration類是繼承于WebMvcConfigurationSupport的:
于是我把@EnableWebMvc這個注解去掉之后再進行嘗試,發(fā)現(xiàn)過濾器生效:
WebMvcConfigurationSupport繼承問題思考
首先我們要對下面的五個類有基本的了解:
- WebMvcConfigurer
- WebMvcConfigurerAdapter
- WebMvcConfigurationSupport
- DelegatingWebMvcConfiguration
- WebMvcConfigurerComposite
關(guān)系如下:
- WebMvcConfigurer 接口提供了很多方法讓我們來定制SpringMVC的配置
- WebMvcConfigurationSupport是一個具體類,其中有很多 @Bean 的方法,注入 SpringMVC 的一些關(guān)鍵組件,方法中會調(diào)用一些空方法,子類只需重寫這些空方法就可以實現(xiàn)定制SpringMVC,而WebMvcConfigurationAdapter則是一個抽象類
- WebMvcConfigurationSupport、WebMvcConfigurationAdapter都可以實現(xiàn)配置SpringMVC,即都可以配置視圖解析器、攔截器以及靜態(tài)資源等
- WebMvcConfigurerAdapter 實現(xiàn)了WebMvcConfigurer ,所有方法實現(xiàn)都是空實現(xiàn),且為抽象類,子類只需覆蓋感興趣的方法即可。
- 在 Spring5.0 開始,WebMvcConfigurer 接口的所有方法都改為了默認方法(基于java8),所以就 不再需要WebMvcConfigurerAdapter類 了,也加上了@Deprecated,子類直接實現(xiàn) WebMvcConfigurer 即可。
- WebMvcConfigurationSupport 支持的自定義的配置更多更全,WebMvcConfigurerAdapter有的方法,這個類也都有。該類是提供MVC Java config 背后配置的主要類。 通常是通過將@EnableWebMvc添加到應(yīng)用程序的@Configuration類中來導(dǎo)入的。 另一個更高級的選擇是直接從此類擴展并在需要時重寫方法,記住子類要添加@Configuration,重寫帶有@Bean的方法也要加上@Bean。
- @EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于擴展了WebMvcConfigurationSupport但是沒有重寫任何方法
- DelegatingWebMvcConfiguration由@EnableWebMvc注解引入,它繼承了WebMvcConfigurationSupport
- 在DelegatingWebMvcConfiguration 類中,有個 @Autowired 的方法 setConfigurers(List<WebMvcConfigurer> configurers),獲取Spring容器的所有 WebMvcConfigurer 類型的bean,存儲到 WebMvcConfigurerComposite 類型的 configurers 屬性中。然后利用上面的 configurers 屬性重寫 WebMvcConfigurationSupport 中所有的空方法
另外,WebMvcConfigurationSupport(@EnableWebMvc)和@EnableAutoConfiguration這兩種方式都有一些默認的設(shè)定。所以有以下幾種使用方式:
-
@Configuration + @EnableWebMvc + extends WebMvcConfigurer
,在擴展的類中重寫父類的方法即可,但會使springboot的@EnableAutoConfiguration自動配置失效 -
@Configuration + extends WebMvcConfigurationSupport
,在擴展的類中重寫父類的方法即可,但會使springboot的@EnableAutoConfiguration自動配置失效 -
@Configuration + extends WebMvcConfigurer
,在擴展的類中重寫父類的方法即可,這種方式依舊使用springboot的@EnableAutoConfiguration中的設(shè)置
WebMvcConfigurationSupport類是徹底自定義配置springmvc,若容器中有該類的子類bean,則springboot的自動配置都會失效,因為WebMvcAutoConfiguration類有 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)文章來源:http://www.zghlxwxcb.cn/news/detail-790306.html
如果還是覺得有點迷糊,那么記住以下兩點即可:文章來源地址http://www.zghlxwxcb.cn/news/detail-790306.html
- 容器中只需要維護一個WebMvcConfigurationSupport即可,多了會失效(最容易出錯的情況就是在使用@EnableWebMvc的情況下,又去繼承WebMvcConfigurationSupport進行配置)。
- 在使用了@EnableWebMvc的情況下,對springmvc的自定義配置實現(xiàn)WebMvcConfigurer是最穩(wěn)妥的,因為DelegatingWebMvcConfiguration會對所以實現(xiàn)了WebMvcConfigurer的配置類進行收集然后去重寫WebMvcConfigurationSupport中的空方法。
到了這里,關(guān)于SpringMVC中的攔截器不生效的問題解決以及衍生出的WebMvcConfigurationSupport繼承問題思考的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!