2023-03-09
處理器 映射器 bean
3 處理器映射器HandlerMapping
HandlerMapping接口負責根據request請求找到對應的Handler處理器及Interceptor攔截器,并將它們封裝在HandlerExecutionChain對象中,返回給中央調度器。
其常用的實現類有兩種:
BeanNameUrlHandlerMapping
SimpleUrlHandlerMapping
3.1 BeanNameUrlHandlerMapping
BeanNameUrlHandlerMapping處理器映射器會根據請求的url與Spring容器中定義的處理器bean的name屬性值進行匹配,從而在Spring容器中找到處理器bean實例。
<!-- 注冊處理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 注冊處理器 -->
<bean name="/hello" class="com.cy.controller.HelloController"></bean>
打開類的源碼,從BeanNameUrlHandlerMapping處理器映射器的方法中可以看出,對于處理器的bean的名稱,必須以“/”開頭,否則無法加入到urls數組中。
public class BeanNameUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping {
/**
* 檢查給定bean的名稱和別名的URL,以“/”開頭。
*/
@Override
protected String[] determineUrlsForHandler(String beanName) {
List<String> urls = new ArrayList<>();
if (beanName.startsWith("/")) {
urls.add(beanName);
}
String[] aliases = obtainApplicationContext().getAliases(beanName);
for (String alias : aliases) {
if (alias.startsWith("/")) {
urls.add(alias);
}
}
return StringUtils.toStringArray(urls);
}
}
使用BeanNameUrlHandlerMapping處理器映射器有兩點明顯不足:
處理器bean的id為一個url請求路徑,而不是bean的名稱,有些不倫不類。
處理器bean的定義與請求url綁定在了一起,若出現多個url請求同一個處理器的情況,就需要在Spring容器中配置多個該處理器類的bean標簽,這將導致容器會創建多個該處理器類實例。
<!-- 注冊處理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 注冊處理器:為一個處理器綁定多個請求url -->
<bean name="/hello" class="com.cy.controller.HelloController"></bean>
<bean name="/world" class="com.cy.controller.HelloController"></bean>
3.2 SimpleUrlHandlerMapping
SimpleUrlHandlerMapping處理器映射器,不僅可以將url與處理器的定義分離,還可以對url進行統一映射管理。
SimpleUrlHandlerMapping處理器映射器會根據請求的url與Spring容器中定義的處理器映射器子標簽的key屬性進行匹配。匹配上后,再將該key的value值與處理器bean的id值進行匹配,從而在Spring容器中找到處理器bean。
<!-- 注冊處理器映射器方式2:SimpleUrlHandlerMapping實現類 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- 方式1:通過mappings屬性為同一個處理器綁定多個url請求,二選一 -->
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
<prop key="/world.do">helloController</prop>
</props>
</property>
<!-- 方式2:通過mappings屬性為同一個處理器綁定多個url請求,二選一 -->
<property name="urlMap">
<map>
<entry key="/hello.action" value="helloController"/>
<entry key="/world.action" value="helloController"/>
</map>
</property>
</bean>
<!-- 注冊處理器 -->
<bean id="helloController" class="com.cy.controller.HelloController"></bean>
<!-- 注冊處理器映射器方式1:BeanNameUrlHandlerMapping實現類 -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> -->
<!-- <bean name="/hello" class="com.cy.controller.HelloController"></bean> -->
<!-- <bean name="/world" class="com.cy.controller.HelloController"></bean> -->
3.3 HandlerMapping源碼分析
HandlerMapping的調用過程見下:
org.springframework.web.servlet.DispatcherServlet(類):
-- void doService(HttpServletRequest request, HttpServletResponse response)
-- doDispatch(request, response)
-- void doDispatch(HttpServletRequest request, HttpServletResponse response)
-- getHandler(processedRequest, false)
-- HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache)
-- getHandler(request)
-- HandlerExecutionChain getHandler(HttpServletRequest request)
-- hm.getHandler(request)
org.springframework.web.servlet.HandlerMapping(接口):
-- HandlerExecutionChain getHandler(HttpServletRequest request)
org.springframework.web.servlet.handler.AbstractHandlerMapping(抽象類 implements HandlerMapping):
-- HandlerExecutionChain getHandler(HttpServletRequest request)
-- getHandlerExecutionChain(handler, request)
-- HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request)
開班時間:2021-04-12(深圳)
開班盛況開班時間:2021-05-17(北京)
開班盛況開班時間:2021-03-22(杭州)
開班盛況開班時間:2021-04-26(北京)
開班盛況開班時間:2021-05-10(北京)
開班盛況開班時間:2021-02-22(北京)
開班盛況開班時間:2021-07-12(北京)
預約報名開班時間:2020-09-21(上海)
開班盛況開班時間:2021-07-12(北京)
預約報名開班時間:2019-07-22(北京)
開班盛況
Copyright 2011-2023 北京千鋒互聯科技有限公司 .All Right
京ICP備12003911號-5
京公網安備 11010802035720號