首页 > 移动平台 > 详细

RequestMappingHandlerMapping--Spring MVC优先级最高的HandlerMapping

时间:2017-02-11 16:44:36      阅读:269      评论:0      收藏:0      [点我收藏+]

DispatcherServlet在启动时会根据配置文件创建HandlerMapping,这些HandlerMapping都实现了Ordered接口,DispatcherServlet实例化所有的HandlerMapping后放到一个集合中,并根据order对HandlerMapping进行排序。

DispatcherServlet在接受请求时会循环遍历有序的HandlerMapping集合,RequestMappingHandlerMapping的order是0,开始接收请求。

RequestMappingHandlerMappin继承AbstractHandlerMethodMapping,AbstractHandlerMethodMapping实现了InitializingBean,在实例化后对调用afterPropertiesSet方法,该方法完成method handler的注册:

Abstracthandlermethodmapping代码:
  1. public void afterPropertiesSet() {  
  2.         initHandlerMethods();  
  3.     }  
  4.   
  5.     /**  
  6.      * Scan beans in the ApplicationContext, detect and register handler methods.  
  7.      * @see #isHandler(Class)  
  8.      * @see #getMappingForMethod(Method, Class)  
  9.      * @see #handlerMethodsInitialized(Map)  
  10.      */  
  11.     protected void initHandlerMethods() {  
  12.         if (logger.isDebugEnabled()) {  
  13.             logger.debug("Looking for request mappings in application context: " + getApplicationContext());  
  14.         }  
  15.   
  16.         String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?  
  17.                 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :  
  18.                 getApplicationContext().getBeanNamesForType(Object.class));  
  19.   
  20.         for (String beanName : beanNames) {  
  21.             if (isHandler(getApplicationContext().getType(beanName))){  
  22.                 detectHandlerMethods(beanName);  
  23.             }  
  24.         }  
  25.         handlerMethodsInitialized(getHandlerMethods());  
  26.     }  

 

RequestMappingHandlerMapping--Spring MVC优先级最高的HandlerMapping

原文:http://www.cnblogs.com/lishichang10/p/6389244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!