《看透SpringMVC》第十二章 HandlerMapping

12 HandlerMapping

12.1 AbstractHandlerMapping

  • 保存所有interceptor
  • 为hanlder添加interceptor

12.2 AbstractUrlHanlderMapping

gethandlerInternal()

通过url找hanlder

  • lookeupHandler()

    • 获取直接映射的(this.handlerMap.get(url))

      • 添加拦截器 PathExposingHandlerInterceptor

        • request添加 BEST_MATCHING_PATTERN_ATTRIBUTE

        • request添加 PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE

        • request添加 INTROSPECT_TYPE_LEVEL_MAPPING(什么东西)

    • 处理带模式匹配的

      • 获取匹配的matchingPatterns
      • patherns排序
      • 多个bestMatch,从中提取url模版变量(应用中什么设置模版变量?)
      • 添加拦截器
        • PathExposingHandlerInterceptor (同上)
        • UriTemplateVariablesHandlerInterceptor

registerHandler()

  • <urlpath, resolvedHanlder> 保存到 this.handlerMap (linkedHashmap)

SimpleUrlHandlerMapping

  • 定义一个urlMap方便配置
  • 自定义的register方法,被initApplicationContext() (WebApplicationObjectSupport方法)调用。

AbstractDetectingUrlHandlerMapping

  • detectHandlersInAncestorContexts 是否从父容器的bean中查找handler

BeanNameUrlHandlerMapping

  • 如果某个bean的名字以“/”开头则,以他为url。

12.3 AbstractHandlerMethodMapping

三个Map

  • handlerMethods pattern->handlerMethod
  • urlMap url->pattern 返回多个pattern,选最优的 (MultiValueMap)
  • nameMap 默认类名里的大写字母组合 + "#" + 方法名 (一般不用), MvcUriComponentsBuilder 根据name获取对应的url
    • 使用RequestMappingInfoHandlerMethodMappingNamingStrategy 从handlerMethod中解析出name。

RequestMappingInfo

七种RequestCondition

initHandlerMethods()

  • 由InitializingBean接口的afterPropertiesSet() 方法调用
  • isHandler() || 判断handler@Controller 或 @RequestMapping
  • detectHandlerMethods()
    • getMappingForMethod(method, userType) || 找到符合handler的methods
    • registerHandlerMethod() || 注册handlerMapping(三个map)
      • MappingRegistry 用于注册
        • 使用ReentrantReadWriteLock (初始化有必要?)

getHandlerInternal()

  • 根据request -> lookupPath
  • lookupPath & Request -> HandlerMethod
  • 如果找到HandlerMethod,返回handlerMethod.createWithResolvedBean() new一个新的实例返回。

猜你喜欢

转载自my.oschina.net/u/1245414/blog/1580664