标签:ide getbean 默认 nbsp xtend strong lookup 模板方法 方法
Interceptor有三个类型的List:
AbstractHandlerMapping的创建其实就是初始化这三个interceptor
private final List<Object> interceptors = new ArrayList<Object>(); private final List<HandlerInterceptor> adaptedInterceptors = new ArrayList<HandlerInterceptor>(); private final List<MappedInterceptor> mappedInterceptors = new ArrayList<MappedInterceptor>();
AbstractHandlerMapping初始化会自动调用initApplicationContext。
@Override protected void initApplicationContext() throws BeansException { //模板方法,用于给子类提供一个添加Interceptors的入口。 extendInterceptors(this.interceptors); //将SpringMvc容器和父容器中所有的MappedInterceptor类型的Bean添加到mappedInterceptors的属性 detectMappedInterceptors(this.mappedInterceptors); //用于初始化Interceptor,将Interceptors属性里所包含的对象按类型添加到mappedInterceptors和adaptedInterceptors. initInterceptors(); }
HandlerMapping是通过getHandler方法来获取处理器Handler和拦截器Interceptors的。下面看下AbstractHandlerMapping的实现。
@Override public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { //1、通过getHandlerInternal方法获取,这是个模板方法。 Object handler = getHandlerInternal(request); if (handler == null) { //2、如果没有获取到则使用默认的Handler handler = getDefaultHandler(); } if (handler == null) { return null; } //3、如果是String类型,则以它为名到SpringMvc的容器里查找相应的Bean。 if (handler instanceof String) { String handlerName = (String) handler; handler = getApplicationContext().getBean(handlerName); } return getHandlerExecutionChain(handler, request); }
getHandlerExecutionChain之前用于找Handler,之后用于添加拦截器。getHandlerExecutionChain的实现使用handler创建出HandlerExecutionChain类型的变量,然后将adaptedInterceptors和符合要求的mappedInterceptors添加进去,最后将其返回。
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) { //创建HandlerExecutionChain HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ? (HandlerExecutionChain) handler : new HandlerExecutionChain(handler)); //添加所有AdaptedInterceptor的拦截器 chain.addInterceptors(getAdaptedInterceptors()); String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); //与请求url进行匹配,满足的才加入 for (MappedInterceptor mappedInterceptor : this.mappedInterceptors) { if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) { chain.addInterceptor(mappedInterceptor.getInterceptor()); } } return chain; }
总结:
AbstractHandlerMapping已经实现了拦截器的初始化,所以子类的主要任务是实现模板方法getHandlerInternal来查找对应的Handler。
欢迎加入Java学习交流群点击:484757838
标签:ide getbean 默认 nbsp xtend strong lookup 模板方法 方法
原文地址:http://www.cnblogs.com/xxjava/p/6913866.html