springMvc源码解读--HandlerMapping

  • HandlerMapping:它的做用是根据request找到相应的处理器handler和interceptors,HandlerMapping接口里面只有一个方法HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;只要使用request就会返回一个HandlerExecutionChain,固然咱们能够定义本身的实现类来实现。
  • HandlerMapping的类的继承结构

clipboard.png
能够看到HandlerMapping家族的成员有两只,一支继承AbstractUrlHandlerMapping,另外一个继承与AbstractHandlerMethodMapping,AbstractHandlerMapping是HandlerMapping的抽象类实现,全部HandlerMapping的实现都继承于AbstractHandlerMapping,AbstractHandlerMapping采用模版的设计模式设计了HandlerMapping实现的总体结构,子类须要经过模版方法提供一些初始值和具体的算法,AbstractHandlerMapping保存了全部的配置的interceptors,在获取到handler后会根据从request提取的lookupPath将相应的interceptors装配上去。java

  • public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport implements HandlerMapping, Ordered 从中可知,AbstractHandlerMapping继承于WebApplicationObjectSupport,初始化的时候会自动的调用initApplicationContext模板方法,

/* java
@Override算法

protected void initApplicationContext() throws BeansException {
    extendInterceptors(this.interceptors); (1)
    detectMappedInterceptors(this.adaptedInterceptors); (2)
    initInterceptors(); (3)
}

*/
其中,extendInterceptors是模版方法,用于给子类提供一个添加interceptors的入口,detectMappedInterceptors方法用于将Spring MVC 的容器及父类容器中的全部MappedInterceptors的bean添加到mappedInterceptors中,initInterceptors方法的做用是初始化interceptor,具体内容实际上是将interceptors属性里所包含的对象按类型添加到MappedInterceptors或adaptedInterceptors设计模式

相关文章
相关标签/搜索