前言
先推荐官方文档给大家,参照官方文档学习。
Spring MVC自动配置
Spring Boot为Spring MVC提供自动配置,适用于大多数应用程序。就是说Spring Boot 自动配置好了SpringMVC。
以下是SpringBoot对SpringMVC的默认配置:都在 (WebMvcAutoConfiguration) 类中
- Inclusion of
ContentNegotiatingViewResolver
andBeanNameViewResolver
beans.- Support for serving static resources, including support for WebJars
- Automatic registration of
Converter
,GenericConverter
, andFormatter
beans.- Support for
HttpMessageConverters
(covered later in this document).- Automatic registration of
MessageCodesResolver
(covered later in this document).- Static
index.html
support.- Custom
Favicon
support (covered later in this document).- Automatic use of a
ConfigurableWebBindingInitializer
bean (covered later in this document).
对上面的翻译:
-
引入
ContentNegotiatingViewResolver
和BeanNameViewResolver
beans。自动配置了
ViewResolver
(视图解析器),即根据方法的返回值得到视图(View)对象,视图对象决定如何渲染(转发或是重定向或是其它)ContentNegotiatingViewResolver
:用于组合所有的视图解析器;所以可以自己给容器中添加一个视图解析器,启动时会自动将其组合进来;自定义视图解析器:1、新建类继承 ViewResolver 类,2、然后重写ViewResolver唯一的方法,3、然后将自己的视图解析器添加到Spring容器中(可以使用@Component或者@Configuration加@Bean)
-
对静态资源的支持,包括对WebJars的支持。
(参考文档官方)
-
自动注册
Converter
,GenericConverter
,Formatter
beans。Converter
:转换器,用于页面提交数据时的类型转换,如提交的字符串“18”后台接收时转为Integer类型Formatter
:格式化器;,用于格式化页面提交的日期数据,如“2017-12-17”接收后要转为Date类型数据GenericConverter
:同样转换器 -
对
HttpMessageConverters
的支持。HttpMessageConverter:SpringMVC 用来转换Http请求和响应的,如控制器方法以Json数据格式返回一个User对象给前端页面
-
自动注册
MessageCodeResolver
。 -
对静态
index.html
的支持。静态首页访问
-
对自定义
Favicon
的支持。配置应用浏览器页面图标
-
自动使用
ConfigurableWebBindingInitializer
bean。
扩展SpringMVC
编写一个配置类,是 WebMvcConfigurerAdapter
类的子类,但是不能标注@EnableWebMvc注解。2.xx版本中已经废弃了 WebMvcConfigurerAdapter
我们可以直接实现 WebMvcConfigurerAdapter 实现的接口 WebMvcConfigurer
这样它既保留了所有的自动配置,也能使用自定义的扩展配置。
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/index.html请求,会直接跳到index页面。
registry.addViewController("/index.html").setViewName("index");
}
}
原理
-
WebMvcAutoConfiguration是SpringMVC的自动配置类,在内部维护了一个内部类WebMvcAutoConfigurationAdapter,该类又实现自WebMvcConfigurer ,看定义:
@Configuration @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class}) @Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
所以自定义的配置类(此例为MyMvcConfig)是个WebMvcConfigurer接口的实现类。
-
在做WebMvcAutoConfigurationAdapter自动配置时会导入@Import(EnableWebMvcConfiguration.class)
-
容器中所有的WebMvcConfigurer都会一起起作用。
-
最后可以结合第2点和第3点看出,自定义的配置类也会被调用。
总结:SpringMVC的自动配置和自定义的扩展配置都会起作用。
全面接管SpringMVC
SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了
我们需要在配置类中添加@EnableWebMvc即可;
使用场景
不推荐全面接管Spring MVC如果,开发一些简单的功能,可以全面接管可以节省内存空间,少配置很多的组件往往,开发需要用到很多的功能一般都会选择,使用自动配置的方式
原理:
为什么@EnableWebMvc自动配置就失效了;
1)@EnableWebMvc的核心
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
2)、
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
3)、
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;
5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;
如何修改SpringBoot的默认配置
模式:
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置
总结
这篇文章主要了解一下MVC 的自动配置,不需要全部掌握