用 WebMvcConfigurer 替代已移除的 WebMvcConfigurerAdapter,并保留 Spring Boot 的 MVC 自动配置。
代码对比
✕ Spring Boot 1.x
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(
ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
✓ Spring Boot 4.x
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(
ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
发现此代码有问题? 告诉我们。
为什么现代方式更好
不再使用已移除的适配器
WebMvcConfigurer 提供默认方法,因此无需扩展适配器超类。
保留 Boot 默认设置
不使用 @EnableWebMvc 时,Spring Boot 会继续自动配置 MVC 基础设施。
只重写必要内容
实现接口,仅自定义应用所需的 MVC 扩展点。
旧方式
带有 @EnableWebMvc 的 WebMvcConfigurerAdapter
现代方式
配合 Spring Boot 自动配置的 WebMvcConfigurer
自 JDK
17
难度
入门
JDK 支持
Spring Boot MVC 配置
可用
在 Spring Boot 4.0 和 Spring Framework 7.0 中可用(需要 Java 17+)
工作原理
Spring Boot 1.x 应用通常扩展 WebMvcConfigurerAdapter,因为当时 Java 接口无法提供默认方法。现代 Spring Framework 版本直接在 WebMvcConfigurer 上提供默认实现,因此只需实现该接口并重写所需方法。在 Spring Boot 应用中,除非有意完全接管 MVC 配置,否则不要添加 @EnableWebMvc:它会禁用 Boot 的 MVC 自动配置,包括常用的转换器、静态资源处理及其他默认设置。
相关文档
证明