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

 

转载于:https://www.cnblogs.com/lishichang10/p/6389244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值