Spring MVC

关于WebMvc的配置

1.最暴力的配置方法就是直接继承WebMvcConfigurationSupport

2.比较温柔的做法就是使用注解@EnableWebMvc

这个注解是引入了DelegatingWebMvcConfiguration这样一个代理配置类,它继承了WebMvcConfigurationSupport,

然后通过注入WebMvcConfigurer来进行配置,所以,只要往容器里装入WebMvcConfigurer的自定义类就可以了。

3.自动配置类WebMvcAutoConfiguration里已经引入了WebMvcAutoConfigurationAdapter,也就是WebMvcConfigurer子类,

同样引入了EnableWebMvcConfiguration,是DelegatingWebMvcConfiguration子类,所以连注解@EnableWebMvc也不需要了。

注意:由于自动配置类WebMvcAutoConfiguration的引入条件是@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

所以,使用了上面1或者2时,是不会自动配置的,这样也就失去了自动配置类做的配置。

由此可见,尽量使用第3种方法,优先使用application.properties来配置,不够的地方,添加WebMvcConfigurer子类配置,

只有实在无法满足要求时才使用2甚至1的方法。

实际上Spring的基本调用也比较简单:

1.通过xml或者注解,创建各种HandlerMapping,这些类的目的就是创建从path到Handler的mapping。

HandlerMapping的唯一一个方法就是getHandler

2.便利各个HandlerMapping,找到Handler后,再遍历各个HandlerAdapter,看看有谁能接受这个Handler。

HandlerAdapter的两个主要方法

boolean supports(Object handler);

ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)

3.有了ModelAndView后,就可以渲染出页面了

①HandlerMapping

BeanNameUrlHandlerMapping

SimpleUrlHandlerMapping//例:/static,/public

RequestMappingHandlerMapping

②HandlerAdapter

Servlet->SimpleServletHandlerAdapter

HttpRequestHandler->HttpRequestHandlerAdapter//例:ResourceHttpRequestHandler

Controller->SimpleControllerHandlerAdapter

HandlerMethod->RequestMappingHandlerAdapter

③HandlerMethodArgumentResolver

// Annotation-based argument resolution

@RequestParam->RequestParamMethodArgumentResolver

RequestParamMapMethodArgumentResolver

@PathVariable->PathVariableMethodArgumentResolver

PathVariableMapMethodArgumentResolver

@MatrixVariable->MatrixVariableMethodArgumentResolver

MatrixVariableMapMethodArgumentResolver

@ModelAttribute->ServletModelAttributeMethodProcessor

@RequestBody->RequestResponseBodyMethodProcessor

@RequestPart->RequestPartMethodArgumentResolver

@RequestHeader->RequestHeaderMethodArgumentResolver

RequestHeaderMapMethodArgumentResolver

@CookieValue->ServletCookieValueMethodArgumentResolver

@Value->ExpressionValueMethodArgumentResolver

@SessionAttribute->SessionAttributeMethodArgumentResolver

@RequestAttribute->RequestAttributeMethodArgumentResolver

// Type-based argument resolution

WebRequest

ServletRequest

MultipartRequest

HttpSession

Principal

Locale

TimeZone

"java.time.ZoneId"

InputStream

Reader

HttpMethod->ServletRequestMethodArgumentResolver

ServletResponse

OutputStream

Writer->ServletResponseMethodArgumentResolver

HttpEntity

RequestEntity->HttpEntityMethodProcessor

RedirectAttributes->RedirectAttributesMethodArgumentResolver

Model->ModelMethodProcessor

Map->MapMethodProcessor

Errors->ErrorsMethodArgumentResolver

SessionStatus->SessionStatusMethodArgumentResolver

UriComponentsBuilder

ServletUriComponentsBuilder->UriComponentsBuilderMethodArgumentResolver

④HandlerMethodReturnValueHandler

ModelAndView->ModelAndViewMethodReturnValueHandler

Model->ModelMethodProcessor

View->ViewMethodReturnValueHandler

StreamingResponseBody

ResponseEntity->StreamingResponseBodyReturnValueHandler

HttpEntity(not RequestEntity)->HttpEntityMethodProcessor

HttpHeaders->HttpHeadersReturnValueHandler

@ModelAttribute->ModelAttributeMethodProcessor

@ResponseBody->RequestResponseBodyMethodProcessor

void || CharSequence->ViewNameMethodReturnValueHandler

Map->MapMethodProcessor

ModelAttributeMethodProcessor

相关推荐