** 如果想在SpringBoot中扩展一些SpringMVC的配置,例如需要配置自定义的视图解析器或拦截器等,需要怎么实现呢?**
例如,自定义一个视图解析器:
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
我们只需要编写一个配置类去实现WebMvcConfigurer接口,并选择实现接口中的方法,不能标注@EnableWebMvc,这些WebMvcConfigurer接口中的方法就是SpringMVC所可以扩展的配置
注意:在SpringBoot1.0版本中扩展SpringMVC配置是继承WebMvcConfigurerAdapter类,但在2.0以上的版本中已经过时,官方推荐使用以上实现WebMvcConfigurer接口的方式进行扩展,因为在2.0版本中WebMvcConfigurer接口有了默认实现。
WebMvcConfigurer方法介绍:这里只列举几个比较关键的方法
public interface WebMvcConfigurer {
//定制URL匹配规则
default void configurePathMatch(PathMatchConfigurer configurer) {
}
//内容协商机制
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
//异步任务执行器。
default void configureAsyncSuppo