RequestMappingHandlerMapping--Spring MVC優先級最高的HandlerMapping


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.     }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM