目录
一、全面接管SpringMVC
二、WebMvcAutoConfiguration 自动配置了的规则
三、@EnableWebMvc 禁用默认行为
四、WebMvcConfigurer 功能
1、SpringBoot 默认配置好了 SpringMVC 的所有常用特性。
2、如果我们需要全面接管SpringMVC的所有配置并禁用默认配置,仅需要编写一个
WebMvcConfigurer
配置类,并标注@EnableWebMvc
即可。3、全手动模式。①、
@EnableWebMvc
: 禁用默认配置。②、WebMvcConfigurer
组件:定义MVC的底层行为。@Configuration @EnableWebMvc //禁用mvc的默认功能 public class MVCConfig implements WebMvcConfigurer { }
1、
WebMvcAutoConfiguration
web场景的自动配置类①、支持RESTful的filter:HiddenHttpMethodFilter。
②、支持非POST请求,请求体携带数据:FormContentFilter。
③、定义了MVC默认的底层行为:
WebMvcConfigurer。
④、
导入EnableWebMvcConfiguration
:、RequestMappingHandlerAdapter:
、
WelcomePageHandlerMapping
: 欢迎页功能支持(模板引擎目录、静态资源目录放index.html),项目访问/ 就默认展示这个页面。、
LocaleResolver
:国际化解析器。、
ThemeResolver
:主题解析器。、
FlashMapManager
:临时数据共享、
FormattingConversionService
: 数据格式化 、类型转化。、
Validator
: 数据校验JSR303
提供的数据校验功能。、
WebBindingInitializer
:请求参数的封装与绑定、
RequestMappingHandlerMapping
:找每个请求由谁处理的映射关系。、
ContentNegotiationManager
:内容协商管理器、
ExceptionHandlerExceptionResolver
:默认的异常解析器⑤、
WebMvcAutoConfigurationAdapter
配置生效,它是一个WebMvcConfigurer
,定义mvc底层组件。、定义好
WebMvcConfigurer
底层组件默认功能;所有功能详见列表、视图解析器:
InternalResourceViewResolver
、视图解析器:
BeanNameViewResolver
,视图名(controller方法的返回值字符串)就是组件名。、内容协商解析器:
ContentNegotiatingViewResolver
、
请求上下文过滤器:RequestContextFilter
: 任意位置直接获取当前请求。、静态资源链规则。
、
ProblemDetailsExceptionHandler
:错误详情。SpringMVC内部场景异常被它捕获:、定义了MVC默认的底层行为:
WebMvcConfigurer
1、
@EnableWebMvc
给容器中导入DelegatingWebMvcConfiguration
组件,他是
WebMvcConfigurationSupport。
2、WebMvcAutoConfiguration
有一个核心的条件注解,@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
,容器中没有WebMvcConfigurationSupport
,WebMvcAutoConfiguration
才生效.3、@EnableWebMvc 导入
WebMvcConfigurationSupport
导致WebMvcAutoConfiguration
失效。导致禁用了默认行为。@EnableWebMVC 禁用了 Mvc的自动配置
WebMvcConfigurer 定义SpringMVC底层组件的功能类
提供方法 |
核心参数 |
功能 |
默认 |
addFormatters |
FormatterRegistry |
格式化器:支持属性上@NumberFormat和@DatetimeFormat的数据类型转换 |
GenericConversionService |
getValidator |
无 |
数据校验:校验 Controller 上使用@Valid标注的参数合法性。需要导入starter-validator |
无 |
addInterceptors |
InterceptorRegistry |
拦截器:拦截收到的所有请求 |
无 |
configureContentNegotiation |
ContentNegotiationConfigurer |
内容协商:支持多种数据格式返回。需要配合支持这种类型的HttpMessageConverter |
支持 json |
configureMessageConverters |
List |
消息转换器:标注@ResponseBody的返回值会利用MessageConverter直接写出去 |
8 个,支持byte,string,multipart,resource,json |
addViewControllers |
ViewControllerRegistry |
视图映射:直接将请求路径与物理视图映射。用于无 java 业务逻辑的直接视图页渲染 |
无 |
configureViewResolvers |
ViewResolverRegistry |
视图解析器:逻辑视图转为物理视图 |
ViewResolverComposite |
addResourceHandlers |
ResourceHandlerRegistry |
静态资源处理:静态资源路径映射、缓存控制 |
ResourceHandlerRegistry |
configureDefaultServletHandling |
DefaultServletHandlerConfigurer |
默认 Servlet:可以覆盖 Tomcat 的DefaultServlet。让DispatcherServlet拦截/ |
无 |
configurePathMatch |
PathMatchConfigurer |
路径匹配:自定义 URL 路径匹配。可以自动为所有路径加上指定前缀,比如 /api |
无 |
configureAsyncSupport |
AsyncSupportConfigurer |
异步支持: |
TaskExecutionAutoConfiguration |
addCorsMappings |
CorsRegistry |
跨域: |
无 |
addArgumentResolvers |
List |
参数解析器: |
mvc 默认提供 |
addReturnValueHandlers |
List |
返回值解析器: |
mvc 默认提供 |
configureHandlerExceptionResolvers |
List |
异常处理器: |
默认 3 个 |
getMessageCodesResolver |
无 |
消息码解析器:国际化使用 |
无 |