Spring源码之请求路径匹配路由方式

目录
  • 请求路径匹配路由
    • 入口
    • 进入上面方法
  • SpringMVC 将请求找到匹配的处理
    • 初始化映射关系
    • 从映射关系中寻找匹配方法

请求路径匹配路由

在spring中,当一个请求过来的时候会做路径匹配,下面我们就从源码层面分析一下路径匹配。

示例:

@RequestMapping(value = "/user/{aid}/online/**", method = RequestMethod.GET)

我们一起看看这个方法是如何寻找的,和一些相应的工具类

入口

我的项目使用的是自动配置的RequestMappingHandlerMapping类,在getHandlerInternal()方法中:

HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);

上面这行是根据你的请求path和request去查找合适的method了。在项目启动的时候,Spring就把路径和对应的方法加载到了内存中。

进入上面方法

  List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
  if (directPathMatches != null) {
   addMatchingMappings(directPathMatches, matches, request);
  }
  if (matches.isEmpty()) {
   // No choice but to go through all mappings...
   addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request);
  }

可以看到如果根据lookupPath直接匹配上了,走第一个方法,如果没有,则需要根据规则匹配,走第二个方法。

mappingRegistry.getMappings().keySer()这个方法获取的类型为RequestMappingInfo类型,后面进入了RequestMappingInfo的getMatchingCondition()方法:

 public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {
  RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request);
  ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request);
  HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request);
  ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request);
  ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request);

  if (methods == null || params == null || headers == null || consumes == null || produces == null) {
   return null;
  }

  PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request);
  if (patterns == null) {
   return null;
  }

可以看到代码里面会查看各种条件是否匹配,包括,请求方法methods,参数params,请求头headers,还出入参类型等相关的consumers,produces等,最后一行就是我们要找的路径匹配patternsCondition.getMatchingCondition(request)。

这个方法会走到PatternRequestCondition的getMatchingPattern方法,然后调用如下方法,获取pattern:

  if (this.pathMatcher.match(pattern, lookupPath)) {
   return pattern;
  }

上面这个pathMatcher的类型就是AntPathMatcher类,就是通过调用AntPathMatcher类的match方法,查看是否匹配,然后返回pattern。

SpringMVC 将请求找到匹配的处理

在SpringMVC的模式下,浏览器的一个请求是如何映射到指定的controller的呢?

初始化映射关系

在web服务器启动时,Spring容器中会保存一个map的数据结构,里边记录这controller和url请求中的对应关系。那么这个map中的数据是如何来的呢?

首先来看AbstractHandlerMethodMapping的initHandlerMethods方法(至于为什么直接找到这个方法,我也是网上搜索的,之前的调用链没去纠结)

protected void initHandlerMethods() {
 if (logger.isDebugEnabled()) {
   logger.debug("Looking for request mappings in application context: " + getApplicationContext());
  }

        //获取Spring容器装配的所有bean的名称
 String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
    BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
    getApplicationContext().getBeanNamesForType(Object.class));

           //遍历
 for (String beanName : beanNames) {
                //判断该bean是否有@controller或者@RequestMapping注解
  if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX) &&
    isHandler(getApplicationContext().getType(beanName))){
                        //如果有上述注解,则需要保存对应关系
   detectHandlerMethods(beanName);
  }
 }
 handlerMethodsInitialized(getHandlerMethods());
}
protected void detectHandlerMethods(final Object handler) {
        //获取传过来handler的类信息
 Class<?> handlerType =
   (handler instanceof String ? getApplicationContext().getType((String) handler) : handler.getClass());

 // Avoid repeated calls to getMappingForMethod which would rebuild RequestMappingInfo instances
        //初始化一个保存映射信息的map
 final Map<Method, T> mappings = new IdentityHashMap<Method, T>();
 final Class<?> userType = ClassUtils.getUserClass(handlerType);

 Set<Method> methods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
  @Override
  public boolean matches(Method method) {
                //获取该类里所有方法的映射信息 T为RequestMappingInfo
                //mapping值的形式为{[/test/test1],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}
   T mapping = getMappingForMethod(method, userType);
   if (mapping != null) {
                                //将信息加入map
    mappings.put(method, mapping);
    return true;
   }
   else {
    return false;
   }
  }
 });

 for (Method method : methods) {
                //注册HandlerMethod,在里边进行一些重复验证等
  registerHandlerMethod(handler, method, mappings.get(method));
 }
}

上述方法中调用了一个比较重要的方法,getMappingForMethod,通过这个方法生成后续我们一直会用到的一个RequestMappingInfo对象。具体方法如下:

@Override
//该方法接收两个参数,一个是具体方法,一个是方法所在的类
 protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
  RequestMappingInfo info = null;
//找到方法的@RequestMapping注解
  RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
  if (methodAnnotation != null) {
//这个方法返回null
   RequestCondition<?> methodCondition = getCustomMethodCondition(method);
//创建RequestMappingInfo对象
   info = createRequestMappingInfo(methodAnnotation, methodCondition);
//找到类的@RequestMapping注解
   RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
   if (typeAnnotation != null) {
//该方法也返回一个null
    RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
//如果类和方法都有@RequestMapping注解,则进行combine操作
    info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
   }
  }
  return info;
 }

那么上述方法中调用的createRequestMappingInfo方法有事如何真正的创建出一个RequestMappingInfo对象的呢?

protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition) {
//拿到@RequestMapping注解上的value值
  String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value());
//创建一个RequestMappingInfo,参数为一堆condition,出了PatternsRequestCondition,其余全部使用@RequestMapping注解上的值
  return new RequestMappingInfo(
    annotation.name(),
    new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(),
      this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions),
    new RequestMethodsRequestCondition(annotation.method()),
    new ParamsRequestCondition(annotation.params()),
    new HeadersRequestCondition(annotation.headers()),
    new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
    new ProducesRequestCondition(annotation.produces(), annotation.headers(), this.contentNegotiationManager),
    customCondition);
 }
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
//handler此处为带有@controller或者@RequestMapping的类的名称
//初始化一个HandlerMethod,包含一些类的名称和方法等信息
  HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
  HandlerMethod oldHandlerMethod = this.handlerMethods.get(mapping);
//判断是否有handlerMethods是否有重复数据,有则抛异常,没有则将其加入handlerMethods map中
  if (oldHandlerMethod != null && !oldHandlerMethod.equals(newHandlerMethod)) {
   throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + newHandlerMethod.getBean() +
     "' bean method \n" + newHandlerMethod + "\nto " + mapping + ": There is already '" +
     oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
  }

  this.handlerMethods.put(mapping, newHandlerMethod);
  if (logger.isInfoEnabled()) {
   logger.info("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
  }

//将没有*号和问好的pattern加入到urlMap中
  Set<String> patterns = getMappingPathPatterns(mapping);
  for (String pattern : patterns) {
   if (!getPathMatcher().isPattern(pattern)) {
    this.urlMap.add(pattern, mapping);
   }
  }

//维护一个nameMap,key为名字,格式为congroller类大写字母+#+方法名
//比如TestBank类的getBank方法,可以为TB#getBank
  if (this.namingStrategy != null) {
   String name = this.namingStrategy.getName(newHandlerMethod, mapping);
   updateNameMap(name, newHandlerMethod);
  }
 }

由上述registerHandlerMethod方法我们可以看出,该方法共维护了三个map分别是:

  • handlermethods: key为RequestMappingInfo value为HandlerMethod
  • urlMap: key为没有*和?的pattern(比如/test/test1)value为RequestMappingInfo
  • nameMap: key为名字,格式为congroller类大写字母+#+方法名,比如TestBank类的getBank方法,key为TB#getBank

上述三个map在后续匹配浏览器请求用哪个方法来处理时会重点用到。

从映射关系中寻找匹配方法

那么DispatcherServlet是如何处理一个请求的呢?

我们从DispatcherServlet的doService方法来看起,该方法中,最终会调用到AbstractHandlerMethodMapping类的lookupHandlerMethod方法来确定这个请求应该由哪个方法处理,代码如下:

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
  List<Match> matches = new ArrayList<Match>();
//从urlMap中寻找能匹配的处理方法
  List<T> directPathMatches = this.urlMap.get(lookupPath);
//如果从urlMap中找到匹配的处理方法,则调用addMatchingMappings方法,将匹配的方法放入matches集合
  if (directPathMatches != null) {
   addMatchingMappings(directPathMatches, matches, request);
  }
//如果urlMap中没有找到直接匹配的方法
  if (matches.isEmpty()) {
   // No choice but to go through all mappings...
   addMatchingMappings(this.handlerMethods.keySet(), matches, request);
  }

  if (!matches.isEmpty()) {
//如果找到了匹配的方法,先获取一个比较器
   Comparator<Match> comparator = new MatchComparator(getMappingComparator(request));
//将匹配到的方法按照比较器排序
   Collections.sort(matches, comparator);
   if (logger.isTraceEnabled()) {
    logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);
   }
//如果成功匹配的方法只有一个,拿这个方法返回。如果匹配到多个方法,取最匹配的前两个进行比较。
//如果比较结果为0,则抛出没有找到唯一合适处理方法的异常
   Match bestMatch = matches.get(0);
   if (matches.size() > 1) {
    Match secondBestMatch = matches.get(1);
    if (comparator.compare(bestMatch, secondBestMatch) == 0) {
     Method m1 = bestMatch.handlerMethod.getMethod();
     Method m2 = secondBestMatch.handlerMethod.getMethod();
     throw new IllegalStateException(
       "Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" +
       m1 + ", " + m2 + "}");
    }
   }
   handleMatch(bestMatch.mapping, lookupPath, request);
   return bestMatch.handlerMethod;
  }
  else {
//没有找到匹配的则返回null
   return handleNoMatch(handlerMethods.keySet(), lookupPath, request);
  }
 }

从上述代码可以看出,程序会先从this.urlMap中寻找是否有匹配的方法,那么这个urlMap中的数据是从什么时候加载的呢?我们网上翻看registerHandlerMethod方法,在web服务器启动时,该方法初始化了urlMap中的数据。

通过上述分析,大致可以了解到Spring容器是如何维护url和方法之间的映射关系,以及当收到请求时又是如何将请求匹配到正确的方法的。

至于没有分析到的当类和方法都有@RequestMapping注解时触发的combine操作究竟做了什么,当找到多个匹配方法是又是如何通过比较器进行排序的,我们下次再分析。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • spring cloud gateway 如何修改请求路径Path

    一.背景 项目升级改造,老项目使用请求url中特定参数进行服务路由,现使用gateway网关进行路由服务信息 二.根据参数信息修改请求路径Path @Component public class RequestFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpR

  • Spring Boot 定制URL匹配规则的方法

    事情的起源:有人问我,说编写了一个/hello访问路径,但是吧,不管是输入/hello还是/hello.html,还是/hello.xxx都能进行访问.当时我还以为他对代码进行处理了,后来发现不是,后来发现这是Spring Boot路由规则.好了,有废话了下,那么看看我们解决上面这个导致的问题. 构建web应用程序时,并不是所有的URL请求都遵循默认的规则.有时,我们希望RESTful URL匹配的时候包含定界符".",这种情况在Spring中可以称之为"定界符定义的格式&q

  • 从源码角度看spring mvc的请求处理过程

    在分析spring mvc源码之前,先看一张图: 请求处理的过程: 1.DispatcherServelt作为前端控制器,拦截request对象. 2.DispatcherServlet接收到request对象之后,查询HandlerMapping,得到一个HandlerExecutionChain对象. 3.DispatcherServlet将Handler对象交由HandlerAdapter(适配器模式的典型应用),调用相应的controller方法. 4.Controller方法返回Mod

  • Spring Cloud GateWay 路由转发规则介绍详解

    Spring在因Netflix开源流产事件后,在不断的更换Netflix相关的组件,比如:Eureka.Zuul.Feign.Ribbon等,Zuul的替代产品就是SpringCloud Gateway,这是Spring团队研发的网关组件,可以实现限流.安全认证.支持长连接等新特性. Spring Cloud Gateway Spring Cloud Gateway是SpringCloud的全新子项目,该项目基于Spring5.x.SpringBoot2.x技术版本进行编写,意在提供简单方便.可

  • Spring源码之请求路径匹配路由方式

    目录 请求路径匹配路由 入口 进入上面方法 SpringMVC 将请求找到匹配的处理 初始化映射关系 从映射关系中寻找匹配方法 请求路径匹配路由 在spring中,当一个请求过来的时候会做路径匹配,下面我们就从源码层面分析一下路径匹配. 示例: @RequestMapping(value = "/user/{aid}/online/**", method = RequestMethod.GET) 我们一起看看这个方法是如何寻找的,和一些相应的工具类 入口 我的项目使用的是自动配置的Re

  • 如何导入spring源码到IDEA

    环境:IDEA2019.3, jdk1.8.0_191,spring-framework-5.0.5.RELEASE,gradle-4.4.1 前期准备 1.访问spring官网下载spring framework源码 spring源码放在了github,点击如下图中的按钮访问(有可能访问不了github官网,可参考此处) 如下图步骤选择要下载的spring源码版本 点击code–>Download ZIP下载 解压到某个目录下 2.安装gradle 到解压的spring源码目录下spring-

  • Spring源码学习之动态代理实现流程

    注:这里不阐述Spring和AOP的一些基本概念和用法,直接进入正题. 流程   Spring所管理的对象大体会经过确定实例化对象类型.推断构造方法创建对象(实例化).设置属性.初始化等等步骤.在对象初始化阶段,Spring为开发者提供了一个BeanPostProcessor接口,它会在对象初始化之前和初始化之后被调用(初始化,不是实例化,对应实例化的是InstantiationAwareBeanPostProcessor接口). public interface BeanPostProcess

  • Spring源码解析之推断构造方法

    Spring推断构造方法 贴个测试代码直接开干,这只是个样例,其他情况自行分析 @Component public class OrderService { public OrderService() { System.out.println("无参构造方法"); } @Autowired(required = false) public OrderService(UserService userService) { System.out.println("一个参数的构造方法

  • 教你使用IDEA搭建spring源码阅读环境的详细步骤

    目录 第一步.准备gradle环境 第二步.下载spring源码 第一步.准备gradle环境 1.去官网下载gradle https://gradle.org/releases/ 2.将其解压缩,创建repository文件夹 和init.d文件夹 创建init.gradle文件 输入文本信息,主要是配置阿里云镜像仓库地址,和maven的类似 gradle.projectsLoaded { rootProject.allprojects { buildscript { repositories

  • spring源码阅读--aop实现原理讲解

    目录 aop实现原理简介 代理实现的处理器(BeanPostProcessor) 代理实现的源头–AnnotationAwareAspectJAutoProxyCreator AnnotationAwareAspectJAutoProxyCreator的继承结构 代理对象(Proxy)的创建 解析并缓存切面 适配切面 aop实现原理简介 首先我们都知道aop的基本原理就是动态代理思想,在设计模式之代理模式中有介绍过这两种动态代理的使用与基本原理,再次不再叙述. 这里分析的是,在spring中是如

  • Spring源码解析 Bean属性填充

    目录 前言 属性填充 执行回调方法及后置处理器 前言 在上一篇文章中,我们分析了Spring中Bean的实例化过程,在结尾我们知道了虽然bean的实例化完成了,但是其中的属性还没有被注入,今天我们就接着来分析属性是如何被注入的. 属性填充 实例化完成后,回到上面第3条的doCreateBean方法中,看一下用BeanWrapper产生的原生对象,里面dao这个属性还是null值. 回归一下之前的代码,接下来要调用populateBean方法进行属性的填充: Object exposedObjec

  • Spring源码分析容器启动流程

    目录 前言 源码解析 1.初始化流程 流程分析 核心代码剖析 2.刷新流程 流程分析 核心代码剖析 前言 本文基于 Spring 的 5.1.6.RELEASE 版本 Spring的启动流程可以归纳为三个步骤: 1.初始化Spring容器,注册内置的BeanPostProcessor的BeanDefinition到容器中 2.将配置类的BeanDefinition注册到容器中 3.调用refresh()方法刷新容器 Spring Framework 是 Java 语言中影响最为深远的框架之一,其

  • spring源码学习之bean的初始化以及循环引用

    实例化方法,把bean实例化,并且包装成BeanWrapper 1.点进这个方法里面. 这个方法是反射调用类中的 factoryMethod 方法. 这要知道@Bean 方法的原理, 实际上spring 会扫描有@bean 注解的方法, 然后把方法名称设置到 BeanDefinition 的 factoryMethod属性中, 接下来就会调到上面截图中的方法实现@Bean 方法的调用. 2. 有参构造函数的时候 determineConstructorsFromBeanPostProcessor

  • Idea 搭建Spring源码环境的超详细教程

    本篇主要讲解如何使用Ideal 搭建Spring的源码环境,想必大家都会多多少少去看过Spring的部分源码,一般我们都是直接点进某个Spring类 然后Idea上面去下载 ,但是确实比较麻烦,而且不能添加自己对源码的注释 理解 ,本篇就来解决这个问题,手把手使用Idea 搭建Spring framework ,并且直接在Spring framework项目中添加我们自己的module 来验证环境是否正确. 本过程会比较耗时 而且容易出错 慢慢来吧. 1. clone spring-framew

随机推荐