title: springMVC
date: 2021-07-03 20:25:10
WebMvcConfigurer和WebMvcConfigurerAdapter和WebMvcConfigurationSupport的区别
-
首先要了解这三个的结构,webMvcConfigurer是顶层接口,WebMvcConfigurerAdapter是webMvcConfigurer的抽象类,WebMvcConfigurationSupport和前面两个什么关系也没有。
-
springboot2.0之后配置拦截器extends 的WebMvcConfigurerAdapter过时,取而代之的是WebMvcConfigurationSupport
-
WebMvcConfigurerAdapter只是对WebMvcCofigurer的空实现,而WebMvcConfigurationSupport的实现的方法更全面,但是继承WebMvcConfigurationSupport会发现Spring Boot的WebMvc自动配置失效(WebMvcAutoConfiguration自动化配置),导致无法视图解析器无法解析并返回到对应的视图.就是
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
会失效
4.因此两者的区别可以简单总结为
- WebMvcConfigurationSupport–>不需要返回逻辑视图,可以选择继承此类
- WebMvcCofigurer–>返回逻辑视图,可以选择实现此方法,重写addInterceptor方法,不一定要继承WebMvcAdapter,可以用自己的类继承,
因为WebMvcAdapter只是webMvcConfigure的空实现类,而2之后webMvcConfigure在内部全是default方法,也就是在接口内就默认实现了,也就不需要webMvcAdapter了
- 详细信息可看这里