内容简介:mvc-HandlerMapping
HandlerMapping:定义了web请求映射和处理器(Handler)之间的映射
类结构:
handler的种类:
Servlet
Controller
HttpRequestHandler (Spring Invoke)
Bean (@RequestMapping)
顶层抽象:
AbstractHandlerMapping:支持sort,默认handler,handler interceptors;关联ApplicationContext ;封装HandlerExecutionChain
URL匹配:
- AbstractUrlHandlerMapping:支持注册url->hander的映射(直接,模糊);handerName->handlerBean;
- AbstractDetectingUrlHandlerMapping:支持从application context中自动发现 handler;启动初始化
- AbstractControllerUrlHandlerMapping:支持判断是否实现 Controller接口,或者Controller 注解修饰
HandlerMethod匹配:
- AbstractHandlerMethodMapping:实现 request和HandlerMethod的映射及注册;InitializingBean 启动初始化
- RequestMappingHandlerMapping: 实现url->RequestMappingInfo->HandlerMethod的映射
具体实现:
- SimpleUrlHandlerMapping:支持直接配置,调用父类注册
- BeanNameUrlHandlerMapping:支持以 /beanName->beanName 的形式注册
- ControllerBeanNameHandlerMapping:
- ControllerClassNameHandlerMapping:
- RequestMappingHandlerMapping:
- DefaultAnnotationHandlerMapping:已经废弃
默认策略:
DispatcherServlet.properties 默认配置:BeanNameUrlHandlerMapping,DefaultAnnotationHandlerMapping
<mvc:annotation-driven/>配置:RequestMappingHandlerMapping,BeanNameUrlHandlerMapping
<mvc:default-servlet-handler/>配置:SimpleUrlHandlerMapping
<mvc:annotation-driven/>配置:SimpleUrlHandlerMapping(Controller接口)
<mvc:view-controller/> 配置: SimpleUrlHandlerMapping (ParameterizableViewController实现类)
DispatcherServlet初始化策略:
private void initHandlerMappings(ApplicationContext context) { this.handlerMappings = null; if (this.detectAllHandlerMappings) { // Find all HandlerMappings in the ApplicationContext, including ancestor contexts. Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false); if (!matchingBeans.isEmpty()) { this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values()); // We keep HandlerMappings in sorted order. OrderComparator.sort(this.handlerMappings); } } else { try { HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class); this.handlerMappings = Collections.singletonList(hm); } catch (NoSuchBeanDefinitionException ex) { // Ignore, we'll add a default HandlerMapping later. } } // Ensure we have at least one HandlerMapping, by registering // a default HandlerMapping if no other mappings are found. if (this.handlerMappings == null) { this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class); if (logger.isDebugEnabled()) { logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default"); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。