Spring 全家桶之 Spring Boot 2.6.4(五)- WebMvcAutoConfiguration(Part D)

四、扩展和修改 Spring Boot Web MVC 配置

Spring Boot官方文档中第 8.1.1. The “Spring Web MVC Framework”小节中提到了如何扩展和完全替代Spring Boot关于Spring Mvc的自动配置

扩展Web MVC配置

Spring Boot在自动配置很多组件的时候,先看容器中有没有用户自定义的配置或组件既那些使用@Bean或@Component注解标注的类,如果没有自定义的组件,才会启用自动配置;也可以将用户自定义的组件和自动配置的组件一起发挥作用。

在使用SSM框架进行Spring MVC的配置时,通常都会在Spring MVC的配置文件中进行配置,如果要进行视图映射配置,可以在配置文件中使用mvc命名空间的view-controller标签配置,也可以在配置文件中配置拦截器等。

在Spring Boot中没有Spring MVC配置文件,如何进行这些配置呢?

官网给出了如下答案:

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

编写一个配置类LilithMvcConfig,使用@Configuration注解,并实现WebMvcConfigurer接口,注意不能标注@EnableWebMVC注解

@Configuration
public class LilithMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 浏览器发送 /lilith 页面跳转到success页面
        registry.addViewController("/lilith").setViewName("success");
    }
}

重新启动应用,浏览器输入 http://localhost:8080/lilith

浏览器输出内容,成功实现了视图映射,也就是原先在Spring MVC 配置文件中实现的功能。

为什么会实现这个功能呢?

首先WebMvcAutoConfiguration是Spring Boot中关于Spring MVC自动配置类,WebMvcAutoConfiguration自动配置类中包含了一个静态类WebMvcAutoConfigurationAdapter

WebMvcAutoConfigurationAdapter也是实现了WebMvcConfigurer,并且在该类中配置类上面提到的视图解析器以及静态资源访问控制等

WebMvcAutoConfigurationAdapter使用@Import注解导入了EnableWebMvcConfiguration类,EnableWebMvcConfiguration继承了DelegatingWebMvcConfiguration,DelegatingWebMvcConfiguration类中包含了一个setConfigurers方法

@Autowire标注在setConfigurers方法上,方法的参数就要从容器中获取,也就是说从容器中获取所有的WebMvcConfigure赋值到configurers中,DelegatingWebMvcConfiguration类中下面所有的配置,比如添加格式转换器

添加转换器就是将容器中所有的Formatter添加到添加都配置中去

所以自定义的HttpMessageConverter和ViewResolver也会起作用,因为也会被遍历到,添加到配置中和Spring Boot本身已经做好的配置一起发挥作用

这就是为什么通过添加@Configuration注解并实现WebMvcConfigure类可以让自定义的配置和Spring Boot中Spring MVC的自动配置同时发挥作用

完全替代Web MVC的自动配置

官网中同时也提到如果你想完全控制Spring MVC,可以在自定义的配置类上添加@EnableWebMVC注解

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

在LilithMvcConfig配置类上添加@EnableWebMvc,重新启动应用,在浏览器上访问index1.html页面

将@EnableWebMvc注解注释,再次启动应用,在浏览器上访问index1.html页面

index1.html页面可以正常访问。而上面添加了@EnableWebMvc注解则无法访问index1.html页面。

@EnableWebMvc使得Spring MVC自动配置失效,静态资源映射规则全部失效,所有的静态资源都无法访问。

为什么@EnableWebMv会导致所有自动配置全部失效

@EnableWebMvc导入了一个DelegatringWebMvcConfiguration类

DelegatringWebMvcConfiguration又继承了WebMvcConfigurationSupport

再回来看WebMvcAutoConfiguration自动配置类上标注了条件注解 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)也就是说,只有在WebMvcConfigurationSupport不在容器中时才会将自动配置类导入容器中。@EnableWebMvc注解往容器中导入了一个WebMvcConfigurationSupport的子类DelegatringWebMvcConfiguration,这就会导致WebMvcAutoConfiguration不会导入到容器中,当然也就无法发挥自动配置的作用了

而@EnableWebMvc注解导入的WebMvcConfigurationSupport的子类DelegatringWebMvcConfiguration只包含了Spring MVC最基本的功能 ;而视图解析器,viewController,拦截器都需要自己配置。

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
Spring 全家桶之 Spring Boot 2.6.4(五)- WebMvcAutoConfiguration(Part D)
Spring Boot官方文档中第 8.1.1. The “Spring Web MVC Framework”小节中提到了如何扩展和完全替代Spring Boo...
<<上一篇
下一篇>>