springmvc RequestMappingHandlerMapping初始化詳解

來源:互聯網
上載者:User

標籤:config   gis   ada   initial   user   抽象方法   遍曆   介面   sre   

 springmvc中配置這個標籤預設註冊三個bean:RequestMappingHandlerMapping,RequestMappingHandlerAdapterDefaultHandlerExceptionResolver

RequestMappingHandlerMapping

 

我們看它實現了InitializingBean 介面,所以在getBean()執行個體化它時會執行afterPropertiesSet()方法,來看該方法幹了什嗎?

方法中執行個體化了一個BuilderConfiguration對象,並為該對象設定了一些路徑抓取器,路徑方法匹配器等。最後還需要調用父類的方法

父類只是調用了initHandlerMethods()方法,該方法很關鍵是將請求路徑和方法匹配的

// 獲得所有的beanName

// 遍曆所有的beanName並獲得type類型

// 判斷類是否被@Controller或是@RequestMapping注釋了,是執行detectHandlerMethods(beanName)方法

 // 首先獲得類型class,根據類的class獲得一個Map,這個map的key是Method,value是RequestMappingInfo,後文詳解#1

 // 遍曆map,獲得Method和RequestMappingInfo,註冊他們。後文詳解#2

 

書接前文#1

 selectMethods()方法中,根據類的class獲得所有的方法,遍曆方法執行doWith(Method method)方法

獲得方法和RequestMappingInfo,將他們存到Map中返回,RequestMappingInfo是怎麼獲得的呢?我們馬上來看

執行的是detectHandlerMethods()方法中的getMappingForMethod(method, userType)方法,該方法是一個抽象方法。RequestMappingHandlerMapping執行方法

// 建立方法的RequestMappingInfo,後文詳解#1-1

// 建立類的RequestMappingInfo,該步驟和上一步如出一轍

// 將前兩者結合,後文詳解#1-2

 書接前文#1-1

// 獲得方法上的@RequestMapping註解的資訊,根據註解建立RequestMappingInfo

 這裡使用了建造者模式建立,我們來看build()方法幹了什嗎?

方法中關鍵的是PatternsRequestCondition對象,該對象的patterns屬性儲存了方法註解的路徑值如 /app,建立了RequestMappingInfo返回

書接前文#1-2

方法中分別使用結合方法,然後重新建立一個返回,我們主要來看一下PatternsRequestCondition的方法

 

使用了AntPathMatcher對象結合類註解路徑和方法註解路徑,如果沒有萬用字元則簡單拼接如 /person , /list --> /person/list

重新建立一個返回

 書接前文#2

AbstractHandlerMethodMapping中使用MappingRegistry屬性對象註冊

 

 // 建立HandlerMethod對象,後文詳解#2-1

// 將映射和對應的方法存起來

 // 獲得映射路徑,將映射路徑和映射對象存起來。後文詳解#2-2

// 獲得RequestMappingInfo的name,將name和method存起來

 // 將映射對象和映射註冊對象存起來

書接前文#2-1

HandlerMethod中,有beanName,beanFactory,method,methodParameter

書接前文#2-2

獲得RequestMappingInfo中的PatternsRequestCondition中的patterns屬性值

 至此RequestMappingHandlerMapping的初始化完成了

 

springmvc RequestMappingHandlerMapping初始化詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.