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