更多信息:
本篇算是学习笔记
编辑器:IntelliJ IDEA 2020.1
目录
The ‘Spring Web MVC framework’
The Spring Web MVC framework (often referred to as simply ‘Spring MVC’) is a rich ‘model view controller’ web framework. Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP requests. Methods in your controller are mapped to HTTP using @RequestMapping annotations.
Here is a typical example @RestController to serve JSON data:
@RestController
@RequestMapping(value="/users")
public class MyRestController {
@RequestMapping(value="/{user}", method=RequestMethod.GET)
public User getUser(@PathVariable Long user) {
// ...
}
@RequestMapping(value="/{user}/customers", method=RequestMethod.GET)
List<Customer> getUserCustomers(@PathVariable Long user) {
// ...
}
@RequestMapping(value="/{user}", method=RequestMethod.DELETE)
public User deleteUser(@PathVariable Long user) {
// ...
}
}
Spring Boot文档
Spring Boot为Spring MVC提供了自动配置,在大多数应用程序中都能很好地工作。
Spring Boot provides auto-configuration for Spring MVC that works well
with most applications.
自动配置在Spring的默认设置之上添加了以下特性:
The auto-configuration adds the following features on top of Spring’s
defaults:
- 包含ContentNegotiatingViewResolver和BeanNameViewResolver bean。
注:自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染)
自己添加的视图解析器,我们只需要放在容器中即可
ContentNegotiatingViewResolve
作用:组合所有的视图解析器的
- Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
- 支持提供静态资源,包括支持webjar(见下文)。
- Support for serving static resources, including support for WebJars (see below).
- 自动注册转换器,通用转换器,格式化器bean。
注:
Converter
转换器是用于类型转换使用,获取所有类型的Converter
;
Formatter
格式化器是用于格式化日期之类的数据,获取所有类型的Formatter;
同上,自己添加的格式化器转换器,我们只需要放在容器中即可
- Automatic registration of Converter, GenericConverter, Formatter beans.
- 支持
HttpMessageConverters
(见下文)。
注:
HttpMessageConverters
:SpringMVC用来转换Http请求和响应的;
HttpMessageConverter
是从容器中确定;获取所有的HttpMessageConverter
;
- Support for HttpMessageConverters (see below).
- MessageCodesResolver的自动注册(见下文)。
注:MessageCodesResolver定义错误代码生成规则
- Automatic registration of MessageCodesResolver (see below).
- 静态 index.html 支持
- Static index.html support.
- 自定义图标支持(见下文)。
- Custom Favicon support (see below).
- 自动使用ConfigurableWebBindingInitializer bean(见下面)。
- Automatic use of a ConfigurableWebBindingInitializer bean (see below).
如果你想保留Spring Boot MVC特性,而你只是想添加额外的MVC配置(拦截器、格式化器、视图控制器等),你可以添加自己的WebMvcConfigurerAdapter类型的@Configuration类,但不需要@EnableWebMvc。如果你想提供RequestMappingHandlerMapping, RequestMappingHandlerAdapter或ExceptionHandlerExceptionResolver的自定义实例,你可以声明一个WebMvcRegistrationsAdapter实例来提供这样的组件。
If you want to keep Spring Boot MVC features, and you just want to add
additional MVC configuration (interceptors, formatters, view
controllers etc.) you can add your own @Configuration class of type
WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to
provide custom instances of RequestMappingHandlerMapping,
RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you
can declare a WebMvcRegistrationsAdapter instance providing such
components.
如果你想完全控制Spring MVC,你可以添加你自己的@Configuration注解@EnableWebMvc。
If you want to take complete control of Spring MVC, you can add your
own @Configuration annotated with @EnableWebMvc.
修改SpringBoot的默认配置
SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个,则将用户配置的和自己默认的组合起来;
在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置
扩展SpringMVC
部分扩展
<!-- 当访问/hello请求的时候,访问success页面-->
<mvc:view-controller path="/hello" view-name="success"/>
<!--拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
编写一个配置类(@Configutation标记类后,类就是一个配置类),是WebMvcConfigurerAdapter (过时了,可以尝试WebMvcConfigurer接口)类型;不能标注@EnableWebMvc
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//既保留了所有的自动配置,也能用我们扩展的配置;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
/**
* 添加视图映射
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求时,来到 success
registry.addViewController("/atguigu").setViewName("success");
}
}
原理:
1)、WebMvcAutoConfiguration是SpringMVC的自动配置类
2)、在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)
EnableWebMvcConfiguration.class的父类
3)、容器中所有的WebMvcConfigurer都会起作用;
4)、我们的配置类继承了WebMvcConfigurerAdapter也是WebMvcConfigurer,我们的配置类也会起作用;
官方说明:
如果你想保留Spring Boot MVC特性,而你只是想添加额外的MVC配置(拦截器、格式化器、视图控制器等),你可以添加自己的WebMvcConfigurerAdapter类型的@Configuration类,但不需要@EnableWebMvc。如果你想提供RequestMappingHandlerMapping, RequestMappingHandlerAdapter或ExceptionHandlerExceptionResolver的自定义实例,你可以声明一个WebMvcRegistrationsAdapter实例来提供这样的组件。
If you want to keep Spring Boot MVC features, and you just want to add
additional MVC configuration (interceptors, formatters, view
controllers etc.) you can add your own @Configuration class of type
WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to
provide custom instances of RequestMappingHandlerMapping,
RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you
can declare a WebMvcRegistrationsAdapter instance providing such
components.
如果你想完全控制Spring MVC,你可以添加你自己的@Configuration注解@EnableWebMvc。
If you want to take complete control of Spring MVC, you can add your
own @Configuration annotated with @EnableWebMvc.
全面接管
SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;
SpringMvc的自动配置失效;
在我们的配置类中添加@EnableWebMvc即可;
当项目的功能少的时候,为了减少内存的消耗可以全面接管,其他情况不推荐。
@EnableWebMvc
4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;而WebMvcAutoConfiguration的注解:
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})发现容器中有WebMvcConfigurationSupport之后,WebMvcAutoConfiguration就只能导入最基本的功能,重要的功能以及不起效了。