分析下springmvc的HandlerMapping映射的抽象类

初始化操作

通过initApplicationContext()方法进行初始化,其一般是由父类执行ApplicationContextAware#setApplicationContext()方法间接调用,源码奉上

	protected void initApplicationContext() throws BeansException {
//供子类扩展添加拦截器,目前spring没有自行实现
extendInterceptors(this.interceptors);
//搜寻springmvc中的MappedInterceptors保存至adaptedInterceptors集合
detectMappedInterceptors(this.adaptedInterceptors);
//将interceptors集合添加至adaptedInterceptors集合中
initInterceptors();
}

主要目的是获取springmvc上下文中的拦截器集合,此处特指MappedInterceptor

AbstractHandlerMapping#getHandler()-获取处理链对象

源码奉上

	public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
//getHandlerInternal(request)方法为抽象方法,供子类实现
//获取到的handler对象一般为bean/HandlerMethod
Object handler = getHandlerInternal(request);
//上述找不到则使用默认的处理类,没有设定则返回null,则会返回前台404错误
if (handler == null) {
handler = getDefaultHandler();
}
if (handler == null) {
return null;
}
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
//创建处理链对象
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
//针对cros跨域请求的处理,此处就不分析了
if (CorsUtils.isCorsRequest(request)) {
CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request);
CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
}
return executionChain;
}

由以上代码分析可知,需要观察下如何获取handler对象以及创建HandlerExecutionChain处理链对象

AbstractHandlerMethodMapping#getHandlerInternal()-针对HandlerMethod的获取

主要是解析@Controller注解类中的@RequestMapping方法得到其中的HandlerMethod对象作为返回的Handler

	protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
//获取访问的路径,一般类似于request.getServletPath()返回不含contextPath的访问路径
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); //获取读锁
this.mappingRegistry.acquireReadLock();
try {
//获取HandlerMethod作为handler对象,这里涉及到路径匹配的优先级
//优先级:精确匹配>最长路径匹配>扩展名匹配
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
//HandlerMethod内部含有bean对象,其实指的是对应的Controller
return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);
}
finally {
//释放读锁
this.mappingRegistry.releaseReadLock();
}
}

AbstractUrlHandlerMapping#getHandlerInternal()-针对beanName的获取

源码奉上,此处的逻辑就较为简单了

	protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
//从handlerMap查找路径对应的beanName
Object handler = lookupHandler(lookupPath, request);
if (handler == null) {
// We need to care for the default handler directly, since we need to
// expose the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE for it as well.
Object rawHandler = null;
if ("/".equals(lookupPath)) {
rawHandler = getRootHandler();
}
if (rawHandler == null) {
rawHandler = getDefaultHandler();
}
if (rawHandler != null) {
// Bean name or resolved handler?
if (rawHandler instanceof String) {
String handlerName = (String) rawHandler;
rawHandler = getApplicationContext().getBean(handlerName);
}
validateHandler(rawHandler, request);
handler = buildPathExposingHandler(rawHandler, lookupPath, lookupPath, null);
}
} return handler;
}

AbstractHandlerMapping#getHandlerExecutionChain()-创建处理链对象

先附上HandlerExecutionChain的内部属性

        //真实处理请求对象
private final Object handler; //拦截器集合
private HandlerInterceptor[] interceptors; //拦截器集合
private List<HandlerInterceptor> interceptorList; //拦截器开始下标,默认正序执行
private int interceptorIndex = -1;

再奉上创建处理链对象的源码

	//此处的handler可为HandlerMethod/beanName
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler)); String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
if (interceptor instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
chain.addInterceptor(mappedInterceptor.getInterceptor());
}
}
else {
chain.addInterceptor(interceptor);
}
}
return chain;
}

处理链的创建基本就是 带有handler对象以及添加拦截器对象集合,用于后期的拦截

  • HandlerExecutionChain处理链内部含有handler对象,可为@RequestMapping对应的HandlerMethod对象,也可为springmvc上下文的beanName

  • HandlerExecutionChain处理链内部主要包含拦截器对象,其可从springmvc上下文获取HandlerInterceptor/MappedHandlerInterceptor类型作为内部集合;

    当然此处获得拦截器集合也是根据路径匹配获取的,比如mvc:interceptor指定的或者mvc:resource指定的

小结

SpringMVC源码情操陶冶-AbstractHandlerMapping的更多相关文章

  1. SpringMVC源码情操陶冶-DispatcherServlet简析(二)

    承接前文SpringMVC源码情操陶冶-DispatcherServlet类简析(一),主要讲述初始化的操作,本文将简单介绍springmvc如何处理请求 DispatcherServlet#doDi ...

  2. SpringMVC源码情操陶冶-AbstractHandlerMethodMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler 类结构瞧一瞧 public abstract ...

  3. SpringMVC源码情操陶冶-AbstractUrlHandlerMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,前文主要讲解了如何获取handler处理对象,本文将针对beanName注册为handler对象作下解析 Abst ...

  4. SpringMVC源码情操陶冶-FreeMarker之web配置

    前言:本文不讲解FreeMarkerView视图的相关配置,其配置基本由FreeMarkerViewResolver实现,具体可参考>>>SpringMVC源码情操陶冶-ViewRe ...

  5. SpringMVC源码情操陶冶-DispatcherServlet

    本文对springmvc核心类DispatcherServlet作下简单的向导,方便博主与读者查阅 DispatcherServlet-继承关系 分析DispatcherServlet的继承关系以及主 ...

  6. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  7. SpringMVC源码情操陶冶-DispatcherServlet父类简析

    阅读源码有助于陶冶情操,本文对springmvc作个简单的向导 springmvc-web.xml配置 <servlet> <servlet-name>dispatch< ...

  8. SpringMVC源码情操陶冶-DispatcherServlet类简析(一)

    阅读源码有利于陶冶情操,此文承接前文SpringMVC源码情操陶冶-DispatcherServlet父类简析 注意:springmvc初始化其他内容,其对应的配置文件已被加载至beanFactory ...

  9. SpringMVC源码情操陶冶-HandlerAdapter适配器简析

    springmvc中对业务的具体处理是通过HandlerAdapter适配器操作的 HandlerAdapter接口方法 列表如下 /** * Given a handler instance, re ...

随机推荐

  1. Codeforces 754A Lesha and array splitting(简单贪心)

    A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...

  2. [51nod1709]复杂度分析

    给出一棵n个点的树(以1号点为根),定义dep[i]为点i到根路径上点的个数.众所周知,树上最近公共祖先问题可以用倍增算法解决.现在我们需要算出这个算法精确的复杂度.我们定义计算点i和点j最近公共组先 ...

  3. HDU 2147 kiki's game(规律,博弈)

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total ...

  4. 51Nod 1004 n^n的末位数字(日常复习快速幂,莫名的有毒,卡mod值)

    1004 n^n的末位数字 题目来源: Author Ignatius.L (Hdu 1061) 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数N,输出 ...

  5. c语言基础学习07

    ============================================================================= 涉及到的知识点有: 1.指针.指针的概念.指 ...

  6. ubuntu配置服务器apache

    在配置apache之前我们需要先配置好ubuntu中的网络,如果不太懂的话可以看看这我的这篇文章:配置ubuntu网络,里面详细的介绍了怎么配置ubuntu的网络. 1.安装apache服务器 sud ...

  7. 浅谈event.client、event.screen与event.offset

    每每看到event.client.event.screen与event.offset这几个,头都大了,今天又碰到了,特来总结下. 1.event.screenX与event.screenY. 首先,e ...

  8. Linux/Unix系统SSH远程按Backspace键删除时出现^H的处理方法

    在linux/unix系统中连接SSH远程工作时,输出字符后按Backspace键删除时,会出现^H,这对习惯了按Backspace键删除的用户来说,感觉非常别扭,虽然可以通过Ctrl+Backspa ...

  9. Python 使用Pillow模块生成验证码

    1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...

  10. Hyperledger Fabric Read-Write set semantics——读写集

    Read-Write set semantics(读写集) 本文讨论了关于读写集当前实现的细节. Transaction simulation and read-write set(事务模拟和读写集) ...