Spring Boot 中自定义 SpringMVC 配置,到底继承谁?
在Spring Boot中,自定义Spring MVC配置通常是通过实现WebMvcConfigurer
接口来完成的。然而,如果你想继承一个已有的配置,例如WebMvcConfigurationSupport
,那么你需要注意,如果继承了WebMvcConfigurationSupport
,则意味着你将取代Spring Boot自动配置的所有Spring MVC特性。
如果你只是想添加一些自定义配置,而不是完全重写,那么实现WebMvcConfigurer
接口是更好的选择。
下面是一个实现WebMvcConfigurer
接口的简单例子:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加自定义拦截器
registry.addInterceptor(...);
}
// 可以继续添加其他需要的配置方法
}
请注意,在大多数情况下,你不需要直接继承WebMvcConfigurationSupport
,除非你需要完全控制Spring MVC的配置。
评论已关闭