我们先理简单梳理一个关系

关系梳理

  1. spring ioc 是spring的核心,用来管理spring bean的生命周期
  2. MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式
  3. spring mvc 是spring的一个独立的模块,就像AOP一样

在spring mvc中把web框架和spring ioc融合在一起,是通过ContextLoaderListener监听servlet上下文的创建后来加载父容器完成的,然后通过配置一个servlet对象DispatcherServlet,在初始化DispatcherServlet时来加载具体子容器,详细的可以参考spring ioc & web宿主 这篇文章

关于我们今天要讲的RequestMappingHandlerMapping也是在DispatcherServlet的初始化过程中自动加载的,默认会自动加载所有实现HandlerMapping接口的bean,且我们可以通过serOrder来设置优先级,系统默认会加载RequestMappingHandlerMapping、BeanNameUrlHandlerMapping、SimpleUrlHandlerMapping 并且按照顺序使用

 1private void initHandlerMappings(ApplicationContext context) {
2 this.handlerMappings = null;
3 if (this.detectAllHandlerMappings) {
4 // Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
5 Map<String, HandlerMapping> matchingBeans =
6 BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
7 if (!matchingBeans.isEmpty()) {
8 this.handlerMappings = new ArrayList<>(matchingBeans.values());
9 // We keep HandlerMappings in sorted order.
10 AnnotationAwareOrderComparator.sort(this.handlerMappings);
11 }
12 }
13}

RequestMappingHandlerMapping 加载过程

  1. RequestMappingHandlerMapping 实现了接口InitializingBean,在bean加载完成后会自动调用afterPropertiesSet方法,在此方法中调用了initHandlerMethods()来实现初始化
  2. 遍历所有bean,如果bean实现带有注解@Controller或者@RequestMapping 则进一步调用detectHandlerMethods处理,处理逻辑大致就是根据@RequestMapping配置的信息,构建RequestMappingInfo,然后注册到MappingRegistry中
 1protected void initHandlerMethods() {
2 String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
3 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(obtainApplicationContext(), Object.class) :
4 obtainApplicationContext().getBeanNamesForType(Object.class));
5 for (String beanName : beanNames) {
6 if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
7 Class<?> beanType = null;
8 beanType = obtainApplicationContext().getType(beanName);
9 if (beanType != null && isHandler(beanType)) {
10 detectHandlerMethods(beanName);
11 }
12 }
13 }
14 handlerMethodsInitialized(getHandlerMethods());
15 }

 1protected void detectHandlerMethods(final Object handler) {
2 Class<?> handlerType = (handler instanceof String ?
3 obtainApplicationContext().getType((String) handler) : handler.getClass());
4 if (handlerType != null) {
5 final Class<?> userType = ClassUtils.getUserClass(handlerType);
6 Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
7 (MethodIntrospector.MetadataLookup<T>) method -> {
8 try {
9 return getMappingForMethod(method, userType);
10 }
11 catch (Throwable ex) {
12 throw new IllegalStateException("Invalid mapping on handler class [" +
13 userType.getName() + "]: " + method, ex);
14 }
15 });
16 methods.forEach((method, mapping) -> {
17 Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
18 registerHandlerMethod(handler, invocableMethod, mapping);
19 });
20 }
21 }

RequestMappingHandlerMapping 解析过程

  1. 在DispatcherServlet中,根据请求对象调用getHander方法获取HandlerExecutionChain对象
  2. 在getHander方法中也是遍历上面默认加载的三个HandlerMapping,当然第一个就是RequestMappingHandlerMapping对象,调用其getHandler方法,根据请求path,找到一个最为匹配的HandlerMethod来处理请求
 1protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
2 if (this.handlerMappings != null) {
3 for (HandlerMapping hm : this.handlerMappings) {
4 if (logger.isTraceEnabled()) {
5 logger.trace(
6 "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
7 }
8 HandlerExecutionChain handler = hm.getHandler(request);
9 if (handler != null) {
10 return handler;
11 }
12 }
13 }
14 return null;
15 }
  1. 根据请求路径获取HandlerInterceptor,然后和上面获得的HandlerMethod一起构成HandlerExecutionChain返回给DispatcherServlet

DispatcherServlet得到HandlerExecutionChain也就获得了处理此次请求所需的Handler【即我们熟悉的Controller和对应的Action】,后续将会选择合适HandlerAdapter来执行对应的Handler,获取返回值,再根据返回值类型,进一步觉决定用什么方式展示给用户,下一遍将开启HandlerAdapter的讲解…….

微信公众号:宋坤明
更多精彩请参考 完整版系列 也可以直接关注我

图注:宋坤明公众号

RequestMappingHandlerMapping 详解的更多相关文章

  1. RequestMappingHandlerMapping详解

    我们先理简单梳理一个关系 关系梳理 spring ioc 是spring的核心,用来管理spring bean的生命周期 MVC 是一种使用 MVC(Model View Controller 模型- ...

  2. spring-mvc注解(mvc:annotation-driver,JSON,配置详解)

    一.DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时! spring 3.1 开始我们应该用 Reque ...

  3. Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解

    一.控制器定义 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求并将其转换为一个模型.在Spring MVC中一个控制器可以包含多个Action(动作. ...

  4. SpringMVC异常处理机制详解[附带源码分析]

    目录 前言 重要接口和类介绍 HandlerExceptionResolver接口 AbstractHandlerExceptionResolver抽象类 AbstractHandlerMethodE ...

  5. HandlerMapping 详解

    HandlerMapping 详解 1. 导言 万丈高楼平地起,SpringMVC的辉煌离不开每个组件的相互协作,上一章详细阐述了SpringMVC整个体系结构及实现原理,知道HandlerMappi ...

  6. 详解SpringMVC请求的时候是如何找到正确的Controller

    详解SpringMVC请求的时候是如何找到正确的Controller[附带源码分析] 目录 前言 源码分析 重要接口介绍 SpringMVC初始化的时候做了什么 HandlerExecutionCha ...

  7. SpringMVC 框架系列之组件概述与配置详解

    在上一篇文章 SpringMVC 框架系列之初识与入门实例 的实例中,我们已经知道,SpringMVC 框架是一个 web 层的框架,本篇文章就详细解释一下 SpringMVC 框架具体文件的配置以及 ...

  8. SpringMVC【开发Controller】详解

    前言 本文主要是讲解在Controller中的开发,主要的知识点有如下: 编码过滤器 使用注解开发 注解@RequestMapping详解 业务方法接收参数 字符串转日期 重定向和转发 返回JSON ...

  9. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

随机推荐

  1. SQL注入总结篇

    分类SQL注入的攻击方式根据应用程序处理数据库返回内容的不同,可以分为可显注入.报错注入和盲注. 可显注入攻击者可以直接在当前界面内容中获取想要获得的内容. 报错注入数据库查询返回结果并没有在页面中显 ...

  2. svn文件管理

    将VS2010工程提交给Git管理时需要哪些文件:    *.h  *.cpp  *.sln  *.vcxproj  *.vcxproj.filters  *.qrc以及Resources目录下的资源 ...

  3. 自定义对象存入Redis

    package com.cms.common; import com.alibaba.fastjson.JSON; import com.qiyi.tvguo.cms.common.utils.Obj ...

  4. VBox&vmware虚拟机安装Linux及Linux基础入门学习

    VBox&vmware虚拟机安装Linux及Linux基础入门学习 通过VMware workstation安装Linux 在安装虚拟机之前,我特意上网搜索了一下目前常使用的虚拟机软件,了解了 ...

  5. 【转载】C++资源之不完全导引

    1,前言 无数次听到“我要开始学习C++!”的呐喊,无数次听到“C++太复杂了,我真的学不会”的无奈.Stan Lippman先生曾在<C++ Primer>一书中指出“C++是最为难学的 ...

  6. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  7. PHP 行为测试工具 Codeception (介绍)

    原文地址:https://phphub.org/topics/25 Codeception 简介 Codeception 简单来说, 分为以下几种测试 Acceptance Tests 验收测试 Fu ...

  8. 【Unity3d】MenuItem修饰的方法无法触发的可能原因

    遇到了MenuItem修饰的方法无法触发的情况,顺利解决. 类放在Editor目录下,该类下其他方法被MenuItem修饰可以触发. 后来发现我修饰的方法和该类下另一个方法重名了. 改方法名,问题解决 ...

  9. CentOS-6.4 minimal - 安装VMware Tools(linux)

    本文参考自:http://www.cnblogs.com/xyq/p/4068018.html 1.挂载光驱 2./mnt下面默认显示以下文件 3.卸载/mnt 4.点击安装VMware Tools ...

  10. jenkins+jacoco+ant自动化代码和应用服务代码分离场景获取远程服务的覆盖率

    前提 自动化代码和应用服务代码分离.jenkins和tomcat服务器分离 思想 1.在tomcat启动javaagent监听. 2.运用其他job_B已部署的应用服务代码 3.拉取自动化代码,开始测 ...