springboot项目中MVCconfiguration配置是继承WebMvcConfigurationSupport还是实现WebMvcConfigurer 呢?这两者有什么区别呢?
WebMvcConfigurationSupport
WebMvcConfigurationSupport 是 Spring MVC 的底层配置类,继承了它,Spring Boot 会检测到你提供了自己的 MVC 配置
- Spring Boot 的 WebMvcAutoConfiguration(自动配置)会完全失效
- 所有默认配置都没了,包括:
-
- ✗ 静态资源映射
-
- ✗ 消息转换器
-
- ✗ 格式化器
-
- ✗ SpringDoc 的自动配置
-
- ✗ 其他很多默认功能
- 继承 WebMvcConfigurationSupport 时,必须手动配置所有资源映射
WebMvcConfigurer
WebMvcConfigurer 是 Spring MVC 的扩展接口
- 实现它只是添加/增强配置,不会覆盖
- Spring Boot 的自动配置继续生效
- 自定义配置会叠加在默认配置之上
- 实现 WebMvcConfigurer 时,Spring Boot 已经自动配置好了:
-
- /static/ 目录自动映射
-
- /webjars/** 自动映射(包括 SpringDoc 的资源)
1 public class WebMvcConfiguration extends WebMvcConfigurationSupport { 2 // 完全覆盖 Spring Boot 的 Web 自动配置 3 } 4 5 public class WebMvcConfiguration implements WebMvcConfigurer { 6 // 只是增强,不会破坏 Spring Boot 的自动配置 7 }
两者作用总结:
| 类/接口 | 作用 | 典型用法 |
|---|---|---|
WebMvcConfigurer |
Spring Boot 提供的扩展点接口,允许你“定制”Spring MVC 的默认行为 | 实现接口(或使用 @Configuration + implements WebMvcConfigurer)添加拦截器、格式化器、CORS 等 |
WebMvcConfigurationSupport |
Spring MVC 的底层配置类基类,相当于自己“接管整个 MVC 配置” | 继承它来自定义 MVC 配置(但会屏蔽 Spring Boot 自动配置) |
核心区别:
| 对比点 | WebMvcConfigurer | WebMvcConfigurationSupport |
|---|---|---|
| 类型 | 接口 | 抽象类 |
| 默认配置 | 不影响 Spring Boot 的自动配置 | 一旦继承它,会禁用 Spring Boot 自动配置(例如静态资源、默认视图解析器等) |
| 使用方式 | 推荐在 @Configuration 类中实现 |
仅在需要完全自定义 MVC 时使用 |
| 多配置共存 | 可以定义多个 WebMvcConfigurer Bean(Spring 会合并) |
只能有一个 WebMvcConfigurationSupport 子类 |
| 常见场景 | 添加拦截器、消息转换器、CORS、自定义参数解析器等 | 编写框架/SDK 或需要完全控制 MVC 配置(很少见) |
推荐用法:
1 @Configuration 2 public class MyWebMvcConfig implements WebMvcConfigurer { 3 4 @Override 5 public void addInterceptors(InterceptorRegistry registry) { 6 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); 7 } 8 9 @Override 10 public void addCorsMappings(CorsRegistry registry) { 11 registry.addMapping("/**").allowedOrigins("*"); 12 } 13 }
高级用法:
1 @Configuration 2 public class MyMvcConfiguration extends WebMvcConfigurationSupport { 3 4 @Override 5 protected void addInterceptors(InterceptorRegistry registry) { 6 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); 7 } 8 }
但这样一来:
-
Spring Boot 默认的静态资源访问(如
/static/**,/public/**)会失效; -
默认的
RequestMappingHandlerAdapter、MessageConverters等配置也会被覆盖; -
需要手动补回全部配置。
因此如果只是加个拦截器或格式化器,不建议用这个。
内部原理简述:
-
WebMvcAutoConfiguration(Spring Boot 提供)默认会加载一套 MVC 配置; -
该配置会自动检测是否存在一个继承
WebMvcConfigurationSupport的类:-
如果有,则跳过 Boot 自动配置;
-
如果没有,则启用默认配置;
-
-
WebMvcConfigurer的实现类只是往默认配置中“追加配置”,不会替换掉原有的。
对比总结:

常见使用坑:
-
❌ 误继承
WebMvcConfigurationSupport→ 导致静态资源、Swagger 页面、favicon 等访问 404 -
❌ 同时存在多个
WebMvcConfigurationSupport子类 → 启动报错或配置混乱 -
✅ 想“局部增强”时实现
WebMvcConfigurer是最稳妥的方案
启动日志对比
extends WebMvcConfigurationSupport:
❌ 没有 SpringDoc 初始化日志 → SpringDoc 没有启动
总结
在 Spring Boot 项目中:
- 🎯 90% 的情况用 implements WebMvcConfigurer(推荐)
- ⚠️ 只有需要完全自定义 MVC 配置时才用 extends WebMvcConfigurationSupport
- 💡 记住:继承会覆盖,实现会增强
| 目的 | 建议使用 |
|---|---|
| 想在 Boot 默认 MVC 上扩展 | ✅ WebMvcConfigurer |
| 想完全控制 MVC 行为(例如做自定义框架、二次封装) | ⚠️ WebMvcConfigurationSupport |
| 想自定义静态资源路径但仍保留默认功能 | ✅ 使用 WebMvcConfigurer 而非继承 Support |