对WebMvcConfigurationSupport类的理解
官方介绍
This is the main class providing the configuration behind the MVC Java config.It is typically imported by adding {@link EnableWebMvc @EnableWebMvc} to an application {@link Configuration @Configuration} class. An alternative more advanced option is to extend directly from this class and override methods as necessary, remembering to add {@link Configuration @Configuration} to the subclass and {@link Bean @Bean} to overridden {@link Bean @Bean} methods.For more details see the javadoc of {@link EnableWebMvc @EnableWebMvc}.
翻译的中文介绍如下:
这是一个在Java MVC配置之后提供配置的主类。它的导入方式是将 @EnableWebMvc注解添加到应用程序@Configuration类。另一个更高级的方式是根据需要覆盖的方法直接extends这个类,但要记住将 @Configuration添加到子类中,将@Bean添加到覆盖的@Bean方法中。有关更多细节,请参见@EnableWebMvc的javadoc。
一、常用的重写接口
/** 解决跨域问题 **/
void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(
ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(
ViewControllerRegistry registry);
/** 静态资源处理 避免静态资源被拦截**/
void addResourceHandlers(
ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer);
1、addResourceHandlers(避免静态资源被拦截)
/**
*
* 功能描述:
* 配置静态资源,避免静态资源请求被拦截
* @auther: vue Su
* @date:
* @param:
* @return:
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/templates/**")
.addResourceLocations("classpath:/templates/");
super.addResourceHandlers(registry);
}
2、addViewControllers(配置视图跳转)
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/");
};
3、addInterceptors(配置拦截器)
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor).addPathPatterns("/*");
super.addInterceptors(registry);
}
二、webMvc自动配置失效
在WebMvcAutoConfiguration类的上方,发现以下注解:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2NMPebk7-1571042402255)(C:\Users\yj020\AppData\Roaming\Typora\typora-user-images\1571033831860.png)]
意思是在spring上下文容器内没有这个bean,那么webMvcAutoConfiguration这个类才会生效。
解决方案是可以使自己的配置类继承WebMvcConfigurerAdapter,但是该类已被废弃,因此在配置mvc时,最好使用以下方式。
三、webMvc配置最佳实践
1、继承WebMvcConfigurer的子抽象类WebMvcConfigurerAdapter
2、配置类上加@EnableWebMvc注解
安全性: 我们大家都知道,抽象类中的方法是不能被子类重写的,因此像一些影响spring框架的方法不会被程序员重写,这样能极大程度的提高框架的稳定性,不会出现手误的程序员重写了不该重写的方法。
3、为什么要添加@EnableWebMvc注解?
@EnableWebMvc注解类上导入了DelegatingWebMvcConfiguration类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iOrKVpLp-1571042402256)(C:\Users\yj020\AppData\Roaming\Typora\typora-user-images\1571035493264.png)]
DelegatingWebMvcConfiguration继承WebMvcConfigurationSupport类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kkIVCgha-1571042402256)(C:\Users\yj020\AppData\Roaming\Typora\typora-user-images\1571035585653.png)]
DelegatingWebMvcConfiguration类除了实例化WebMvcConfigurationSupport实例以外,另一个作用就是收集BeanFactory中所有WebMvcConfigurer的实现,汇集到WebMvcConfigurerComposite中,在WebMvcConfigurationSupport实例化过程中会分别调用这些实现,将相应的实例传入这些实现中,供开发者在此基础上添加自定义的配置。
注意:在springboot下自定义的WebMvcConfigurer的实现类(用户自己的配置类)上是不需要添加@EnableWebMvc的,因为springboot已经实例化了WebMvcConfigurationSupport。