AbstractHandlerMethodMapping是Spring MVC中所有HandlerMapping的基类,它实现了HandlerMapping接口,并提供了一些公共的方法和属性,用于处理请求和映射处理方法。

1. 属性

AbstractHandlerMethodMapping定义了以下属性:

- handlerMethods:一个Map,用于存储请求路径和对应的处理方法。

- urlLookup:一个Map,用于存储请求路径和对应的处理方法的映射关系。

- pathMatcher:一个PathMatcher对象,用于匹配请求路径和处理方法的映射关系。

- corsConfigurationSource:一个CorsConfigurationSource对象,用于处理跨域请求。

2. 方法

AbstractHandlerMethodMapping定义了以下方法:

- setPathMatcher(PathMatcher pathMatcher):设置PathMatcher对象。

- setCorsConfigurationSource(CorsConfigurationSource corsConfigurationSource):设置CorsConfigurationSource对象。

- registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping):注册处理方法。

- lookupHandlerMethod(String lookupPath, HttpServletRequest request):查找处理方法。

- getCorsConfiguration(Object handler, HttpServletRequest request):获取跨域配置信息。

3. registerHandlerMethod方法

registerHandlerMethod方法用于注册处理方法,它接收三个参数:

- handler:处理方法所属的Controller对象。

- method:处理方法。

- mapping:RequestMappingInfo对象,包含了请求路径、请求方法、请求参数等信息。

registerHandlerMethod方法首先会根据RequestMappingInfo对象生成一个HandlerMethod对象,然后将HandlerMethod对象存储到handlerMethods属性中。同时,它还会将请求路径和HandlerMethod对象的映射关系存储到urlLookup属性中。

4. lookupHandlerMethod方法

lookupHandlerMethod方法用于查找处理方法,它接收两个参数:

- lookupPath:请求路径。

- request:HttpServletRequest对象。

lookupHandlerMethod方法首先会根据请求路径查找urlLookup属性中的映射关系,如果找到了对应的HandlerMethod对象,则直接返回。否则,它会遍历handlerMethods属性中的所有HandlerMethod对象,使用PathMatcher对象匹配请求路径和HandlerMethod对象的映射关系,如果找到了匹配的HandlerMethod对象,则将其存储到urlLookup属性中,并返回。

5. getCorsConfiguration方法

getCorsConfiguration方法用于获取跨域配置信息,它接收两个参数:

- handler:处理方法所属的Controller对象。

- request:HttpServletRequest对象。

getCorsConfiguration方法首先会判断corsConfigurationSource属性是否为空,如果为空,则返回null。否则,它会调用corsConfigurationSource对象的getCorsConfiguration方法获取跨域配置信息,并返回。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!