RequestMappingHandlerMapping 详解
我们先理简单梳理一个关系
关系梳理
- spring ioc 是spring的核心,用来管理spring bean的生命周期
- MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式
- 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 加载过程
- RequestMappingHandlerMapping 实现了接口InitializingBean,在bean加载完成后会自动调用afterPropertiesSet方法,在此方法中调用了initHandlerMethods()来实现初始化
- 遍历所有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 解析过程
- 在DispatcherServlet中,根据请求对象调用getHander方法获取HandlerExecutionChain对象
- 在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 }
- 根据请求路径获取HandlerInterceptor,然后和上面获得的HandlerMethod一起构成HandlerExecutionChain返回给DispatcherServlet
DispatcherServlet得到HandlerExecutionChain也就获得了处理此次请求所需的Handler【即我们熟悉的Controller和对应的Action】,后续将会选择合适HandlerAdapter来执行对应的Handler,获取返回值,再根据返回值类型,进一步觉决定用什么方式展示给用户,下一遍将开启HandlerAdapter的讲解…….
微信公众号:宋坤明
更多精彩请参考 完整版系列 也可以直接关注我
图注:宋坤明公众号
RequestMappingHandlerMapping 详解的更多相关文章
- RequestMappingHandlerMapping详解
我们先理简单梳理一个关系 关系梳理 spring ioc 是spring的核心,用来管理spring bean的生命周期 MVC 是一种使用 MVC(Model View Controller 模型- ...
- spring-mvc注解(mvc:annotation-driver,JSON,配置详解)
一.DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时! spring 3.1 开始我们应该用 Reque ...
- Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解
一.控制器定义 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求并将其转换为一个模型.在Spring MVC中一个控制器可以包含多个Action(动作. ...
- SpringMVC异常处理机制详解[附带源码分析]
目录 前言 重要接口和类介绍 HandlerExceptionResolver接口 AbstractHandlerExceptionResolver抽象类 AbstractHandlerMethodE ...
- HandlerMapping 详解
HandlerMapping 详解 1. 导言 万丈高楼平地起,SpringMVC的辉煌离不开每个组件的相互协作,上一章详细阐述了SpringMVC整个体系结构及实现原理,知道HandlerMappi ...
- 详解SpringMVC请求的时候是如何找到正确的Controller
详解SpringMVC请求的时候是如何找到正确的Controller[附带源码分析] 目录 前言 源码分析 重要接口介绍 SpringMVC初始化的时候做了什么 HandlerExecutionCha ...
- SpringMVC 框架系列之组件概述与配置详解
在上一篇文章 SpringMVC 框架系列之初识与入门实例 的实例中,我们已经知道,SpringMVC 框架是一个 web 层的框架,本篇文章就详细解释一下 SpringMVC 框架具体文件的配置以及 ...
- SpringMVC【开发Controller】详解
前言 本文主要是讲解在Controller中的开发,主要的知识点有如下: 编码过滤器 使用注解开发 注解@RequestMapping详解 业务方法接收参数 字符串转日期 重定向和转发 返回JSON ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
随机推荐
- django数据库迁移-15
目录 1.迁移 1.生成迁移文件 2.执行迁移命令 添加测试数据 1.迁移 创建完模型类后,并没有真正的在数据库中创建了数据表,需要执行迁移命令,在数据表中创建数据表. 1.生成迁移文件 manage ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- node.js之express中app.use
express中app.use 用法: app.use([path,] function [, function…]) 一.app.use() 在express中是怎么工作的 app.use在expr ...
- Makefile:如何写目标依赖
本文并不是Makefile的教程,仅是本人学习时的感悟. Makefile的基本格式 目标:依赖文件(or 目标) [tab]命令 目标: 是要生成的或操作的命令的索引 依赖: 是生成目标依赖的文件或 ...
- 20155212 ch02 课下作业
20155212 ch02 课下作业 T1 题目 参考附图代码,编写一个程序 "week0601学号.c",判断一下你的电脑是大端还是小端 相关知识 小端法:最低有效字节在最前面 ...
- OO原则汇总
SOLID原则:http://www.cnblogs.com/lanxuezaipiao/archive/2013/06/09/3128665.html https://www.cnblogs.com ...
- [arc065E]Manhattan Compass[曼哈顿距离和切比雪夫距离转换]
Description 传送门 Solution 题目要求的是曼达顿距离,对于每个点(x,y),我们把它变为(x-y,x+y),就可以转换成求切比雪夫距离了. 证明如下:$max(\left | (x ...
- js Date对象要注意的问题(时间转换)
1.时间戳和时间对象可以灵活转变: let n = new Date() // 返回的是当前时间对应的国际时间 let nt =n.getTime() let n2 =new Date(nt) con ...
- WCF中数据契约之已知类型的几种公开方式
WCF中传输的数据不想传统的面向对象编程,它只传递了一些对象的属性,但是自身并不知道自己属于什么对象,所以,他没有子类和父类的概念,因而也就没有Is-a的关系,所以在WCF中,如果想维持这种继承关系, ...
- Mysql 5.5从零开始学阅读笔记
第一章 1.什么是数据库? 数据库包含两层含义:保管数据的“仓库”,以及数据管理的方法和技术. 2.表 行被称为记录,列被称为字段 3.主键 primary key,用于唯一标识表中的每一条记录,主键 ...