当前位置: 首页 > news >正文

长沙网站设计培训机构有人上相亲网站做传销燕窝

长沙网站设计培训机构,有人上相亲网站做传销燕窝,深圳制作公司网页,iis怎么让添加的网站没有端口前面分析了 request 与 handler method 映射关系的注册,现在再来分析一下 SpringMVC 是如何根据 request 来获取对应的 handler method 的? 可能有人会说,既然已经将 request 与 handler method 映射关系注册保存在了 AbstractHandlerMethodMapping.Ma…

前面分析了 request 与 handler method 映射关系的注册,现在再来分析一下 SpringMVC 是如何根据 request 来获取对应的 handler method 的?

可能有人会说,既然已经将 request 与 handler method 映射关系注册保存在了 AbstractHandlerMethodMapping.MappingRegistry#registry 中,那么根据 request 不就能直接从 registry 中获取到相应的 handler method 了吗?

如果我们定义的 Controller 中的 @RequestMapping 都是普通的字符的话,那确实是可以直接通过 registry 获取 handler method。
但是,@RequestMapping 还支持占位符、通配符等 url,例如:

@RequestMapping("/resources/ima?e.png") // 匹配路径段中的一个字符
@RequestMapping("/resources/*.png") // 匹配路径段中的零个或多个字符
@RequestMapping("/resources/**") // 匹配多个路径段
@RequestMapping("/projects/{project}/versions") // 匹配路径段并将其捕获为变量
@RequestMapping("/projects/{project:[a-z]+}/versions") // 使用正则表达式匹配并捕获变量

所以,查找 request 对应的 handler method 就不是那么简单的事情了。

如何通过 request 获取 handler?

通过对 DispatcherServlet 的分析,我们知道 SpringMVC 获取 handler 的方法如下:
org.springframework.web.servlet.DispatcherServlet#getHandler()

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {if (this.handlerMappings != null) {for (HandlerMapping mapping : this.handlerMappings) {HandlerExecutionChain handler = mapping.getHandler(request);if (handler != null) {return handler;}}}return null;
}

可以看出,SpringMVC 是通过 HandlerMapping#getHandler() 来获取 request 对应的 handler method 处理程序的,最终会拿到一个 HandlerExecutionChain

HandlerExecutionChain 中包含了 request 对应的 handler method 和 interceptors。

HandlerMapping 的类图如下:
HandlerMapping.png

HandlerMapping 的实现类中其中最常用的是 RequestMappingHandlerMapping,它是专门用来处理 @RequestMapping 定义的 request 请求映射的。

获取 HandlerExecutionChain 的过程

HandlerExecutionChain 的获取是在 AbstractHandlerMapping#getHandler() 中完成的。
主要分成了两步:
1、获取 request 对应的 handler 程序
2、将 handler 和 相应的 HandlerInterceptors 组装成 HandlerExecutionChain

getHandler.png

获取 request 对应的 handler method 的过程

上一步获取 HandlerExecutionChain 时,会调用 AbstractHandlerMethodMapping#getHandlerInternal() 来获取 request 对应的 handler method。
最终它会调用到 AbstractHandlerMethodMapping#lookupHandlerMethod() 来获取。

lookupHandlerMethod.png

可以看到,AbstractHandlerMethodMapping#lookupHandlerMethod 实现了 request 与 handler method 映射关系的查找:
1、首先,通过 directPath 直接获取映射的 handler method
2、如果通过 directPath 没有找到的话,就循环 registry 中所有的映射,查出映射关系
3、如果获取到的映射的 handler 个数 > 1 的话,就找出最合适的匹配

DirectPath: 非 patterns 的 path。
非 patterns 的 path 是指 path 的定义中没有 ?、*、{} 的 path

通过 lookupPath 获取 HandlerMethod

AbstractHandlerMethodMapping#lookupHandlerMethod() 在寻找 HandlerMethod 时,有两种逻辑:

  • 1、直接通过 directPath 快速查找
  • 2、循环所有的映射关系,查找 RequestCondition 条件匹配,获取最合适的匹配

直接通过 directPath 快速查找

directPath 是最快速的方式,直接从 map 中获取匹配结果。
directPath 是不包含占位符、模式匹配符的普通路径,所以这种普通路径是最直接,也是效率最高的。

循环所有的映射关系,通过 RequestCondition 条件匹配,获取最合适的匹配

如果通过 directPath 没有找到任何匹配的话,就需要遍历所有注册的映射关系,找出最合适的匹配。


addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);// 这里会循环所有注册的映射关系,将满足 RequestCondition 的映射查找出来,存放到 matches 中
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {for (T mapping : mappings) {T match = getMatchingMapping(mapping, request);if (match != null) {matches.add(new Match(match, this.mappingRegistry.getRegistrations().get(mapping)));}}
}

对于 @RequestMapping 注解形式的 url 映射,AbstractHandlerMethodMapping#getMatchingMapping() 最终会调用实现类的方法 RequestMappingInfoHandlerMapping#getMatchingCondition() 来获取匹配。

getMatchingCondition.png

可以看到,Spring 会检查 RequestMappingInfo 是否满足以下 8 个条件:
1、请求方法 RequestMethod 是否匹配,如:GET, POST, PUT, DELETE 等
2、请求参数 params 是否匹配
3、请求头 header 是否匹配
4、consumes Content-Type 是否匹配
5、produces Content-Type 是否匹配
6、处理 directPath 和 通配符的请求映射匹配
7、处理 ant 风格的请求映射匹配
8、处理自定义的请求映射条件匹配

关于 RequestCondition 的详细分析查看 @RequstMapping和RequestCondition

小结

SpringMVC 是通过 HandlerMapping#getHandler() 来获取 request 对应的 handler method 处理程序的。
底层实现是通过 AbstractHandlerMethodMapping#lookupHandlerMethod 来查找 request 对应的 handler method:
1、首先,通过 directPath 直接获取映射的 handler method
2、如果通过 directPath 没有找到的话,就循环 registry 中所有的映射,查出映射关系
3、如果获取到的映射的 handler 个数 > 1 的话,就找出最合适的匹配

对于 url 映射中的通配符、ant 风格的请求,都是通过 RequestCondition 接口来进行处理的。

http://www.yayakq.cn/news/491341/

相关文章:

  • 西宁网站建设报价壹君博贴心国内经典网站
  • 未备案网站 赚钱养老院网站建设的好处
  • 网站正在建设 英文翻译微信网站开发用什么语言
  • 个人网站备案方法泉州建设工程招投标信息网
  • 四平网站公司帝国 网站搬家
  • 山西网站建设服务合肥seo排名优化公司
  • 如何用模板建网站优化营商环境
  • 网页设计需要学什么技术北京seo案例
  • 甘肃建设投资集团控股有限网站分销系统大概多少钱
  • 上海网站关键排名免费查看采购信息的平台
  • 网站建设页面图wordpress上传图片路径修改
  • 起名网站是怎么做的短网址生成器免费
  • 做网站常见程序局域网如何做网站
  • 现在用什么做网站怎么注册公司需要什么手续
  • 网站导航固定代码o2o网站建设价格
  • vps网站被镜像应用商店下载安装到桌面
  • 苏州网站搜索排名云南网app
  • 中国风格网站化妆品网站开发背景
  • 网站建设和优化的步骤沈阳网站公司哪个好
  • 电子工程师网站浏览不良网页的危害
  • 网站开发任务分解人才网网站开发手册
  • 这几年做那个网站能致富网站挂马怎么处理
  • 哈尔滨做网站多少钱做招商网站
  • 增加网站关键词库想注册个网站做短租房投资多少钱
  • 石家庄网站建设吧wordpress 满屏主题
  • 德州市经济开发区建设局网站wordpress菜单新连接
  • 商丘网站建设模板原创wordpress付费主题
  • 网站添加微信iis停止单个网站
  • 装饰工程东莞网站建设网页设计和平面设计哪个工资高
  • 网站建设收费标准服务外贸网站建设 福田