SpringMVC之Controller查找的示例分析

這篇文章將為大家詳細(xì)講解有關(guān)Spring MVC之Controller查找的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)2013年至今,先為泰寧等服務(wù)建站,泰寧等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為泰寧企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

1 SpringMVC請(qǐng)求流程

Spring MVC之Controller查找的示例分析

Controller查找在上圖中對(duì)應(yīng)的步驟1至2的過(guò)程

Spring MVC之Controller查找的示例分析
SpringMVC詳細(xì)運(yùn)行流程圖

2 SpringMVC初始化過(guò)程

2.1 先認(rèn)識(shí)兩個(gè)類

1.RequestMappingInfo

封裝RequestMapping注解

包含HTTP請(qǐng)求頭的相關(guān)信息

一個(gè)實(shí)例對(duì)應(yīng)一個(gè)RequestMapping注解

2.HandlerMethod

封裝Controller的處理請(qǐng)求方法

包含該方法所屬的bean對(duì)象、該方法對(duì)應(yīng)的method對(duì)象、該方法的參數(shù)等

Spring MVC之Controller查找的示例分析

RequestMappingHandlerMapping的繼承關(guān)系

在SpringMVC初始化的時(shí)候

首先執(zhí)行RequestMappingHandlerMapping的afterPropertiesSet

然后進(jìn)入AbstractHandlerMethodMapping的afterPropertiesSet

這個(gè)方法會(huì)進(jìn)入該類的initHandlerMethods

負(fù)責(zé)從applicationContext中掃描beans,然后從bean中查找并注冊(cè)處理器方法

//Scan beans in the ApplicationContext, detect and register handler methods.
protected void initHandlerMethods() {
 ...
 //獲取applicationContext中所有的bean name
 String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
 getApplicationContext().getBeanNamesForType(Object.class));
 
 //遍歷beanName數(shù)組
 for (String beanName : beanNames) {
 //isHandler會(huì)根據(jù)bean來(lái)判斷bean定義中是否帶有Controller注解或RequestMapping注解
 if (isHandler(getApplicationContext().getType(beanName))){
 detectHandlerMethods(beanName);
 }
 }
 handlerMethodsInitialized(getHandlerMethods());
}

Spring MVC之Controller查找的示例分析

RequestMappingHandlerMapping#isHandler

上圖方法即判斷當(dāng)前bean定義是否帶有Controlller注解或RequestMapping注解

如果只有RequestMapping生效嗎?不會(huì)的!

因?yàn)檫@種情況下Spring初始化的時(shí)候不會(huì)把該類注冊(cè)為Spring bean,遍歷beanNames時(shí)不會(huì)遍歷到這個(gè)類,所以這里把Controller換成Compoent也可以,不過(guò)一般不這么做

當(dāng)確定bean為handler后,便會(huì)從該bean中查找出具體的handler方法(即Controller類下的具體定義的請(qǐng)求處理方法),查找代碼如下

 /**
 * Look for handler methods in a handler
 * @param handler the bean name of a handler or a handler instance
 */
protected void detectHandlerMethods(final Object handler) {
 //獲取當(dāng)前Controller bean的class對(duì)象
 Class<?> handlerType = (handler instanceof String) ?
 getApplicationContext().getType((String) handler) : handler.getClass();
 //避免重復(fù)調(diào)用 getMappingForMethod 來(lái)重建 RequestMappingInfo 實(shí)例
 final Map<Method, T> mappings = new IdentityHashMap<Method, T>();
 //同上,也是該Controller bean的class對(duì)象
 final Class<?> userType = ClassUtils.getUserClass(handlerType); 
 //獲取當(dāng)前bean的所有handler method
 //根據(jù) method 定義是否帶有 RequestMapping 
 //若有則創(chuàng)建RequestMappingInfo實(shí)例
 Set<Method> methods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
  @Override
  public boolean matches(Method method) {
  T mapping = getMappingForMethod(method, userType);
  if (mapping != null) {
   mappings.put(method, mapping);
   return true;
  }
  else {
   return false;
  }
  }
 });

 //遍歷并注冊(cè)當(dāng)前bean的所有handler method
 for (Method method : methods) {
  //注冊(cè)handler method,進(jìn)入以下方法
  registerHandlerMethod(handler, method, mappings.get(method));
 }

以上代碼有兩個(gè)地方有調(diào)用了getMappingForMethod

使用方法和類型級(jí)別RequestMapping注解來(lái)創(chuàng)建RequestMappingInfo

 @Override
 protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
 RequestMappingInfo info = null;
 //獲取method的@RequestMapping
 RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
 if (methodAnnotation != null) {
  RequestCondition<?> methodCondition = getCustomMethodCondition(method);
  info = createRequestMappingInfo(methodAnnotation, methodCondition);
  //獲取method所屬bean的@RequtestMapping注解
  RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
  if (typeAnnotation != null) {
  RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
  //合并兩個(gè)@RequestMapping注解
  info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
  }
 }
 return info;
 }

這個(gè)方法的作用就是根據(jù)handler method方法創(chuàng)建RequestMappingInfo對(duì)象。首先判斷該mehtod是否含有RequestMpping注解。如果有則直接根據(jù)該注解的內(nèi)容創(chuàng)建RequestMappingInfo對(duì)象。創(chuàng)建以后判斷當(dāng)前method所屬的bean是否也含有RequestMapping注解。如果含有該注解則會(huì)根據(jù)該類上的注解創(chuàng)建一個(gè)RequestMappingInfo對(duì)象。然后在合并method上的RequestMappingInfo對(duì)象,最后返回合并后的對(duì)象?,F(xiàn)在回過(guò)去看detectHandlerMethods方法,有兩處調(diào)用了getMappingForMethod方法,個(gè)人覺(jué)得這里是可以優(yōu)化的,在第一處判斷method時(shí)否為handler時(shí),創(chuàng)建的RequestMappingInfo對(duì)象可以保存起來(lái),直接拿來(lái)后面使用,就少了一次創(chuàng)建RequestMappingInfo對(duì)象的過(guò)程。然后緊接著進(jìn)入registerHandlerMehtod方法,如下

protected void registerHandlerMethod(Object handler, Method method, T mapping) {
 //創(chuàng)建HandlerMethod
 HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
 HandlerMethod oldHandlerMethod = handlerMethods.get(mapping);
 //檢查配置是否存在歧義性
 if (oldHandlerMethod != null && !oldHandlerMethod.equals(newHandlerMethod)) {
  throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + newHandlerMethod.getBean()
   + "' bean method \n" + newHandlerMethod + "\nto " + mapping + ": There is already '"
   + oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
 }
 this.handlerMethods.put(mapping, newHandlerMethod);
 if (logger.isInfoEnabled()) {
  logger.info("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
 }
 //獲取@RequestMapping注解的value,然后添加value->RequestMappingInfo映射記錄至urlMap中
 Set<String> patterns = getMappingPathPatterns(mapping);
 for (String pattern : patterns) {
  if (!getPathMatcher().isPattern(pattern)) {
  this.urlMap.add(pattern, mapping);
  }
 }
}

這里T的類型是RequestMappingInfo。這個(gè)對(duì)象就是封裝的具體Controller下的方法的RequestMapping注解的相關(guān)信息。一個(gè)RequestMapping注解對(duì)應(yīng)一個(gè)RequestMappingInfo對(duì)象。HandlerMethod和RequestMappingInfo類似,是對(duì)Controlelr下具體處理方法的封裝。先看方法的第一行,根據(jù)handler和mehthod創(chuàng)建HandlerMethod對(duì)象。第二行通過(guò)handlerMethods map來(lái)獲取當(dāng)前mapping對(duì)應(yīng)的HandlerMethod。然后判斷是否存在相同的RequestMapping配置。如下這種配置就會(huì)導(dǎo)致此處拋
Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map...
異常

@Controller
@RequestMapping("/AmbiguousTest")
public class AmbiguousTestController {
 @RequestMapping(value = "/test1")
 @ResponseBody
 public String test1(){
  return "method test1";
 }
 @RequestMapping(value = "/test1")
 @ResponseBody
 public String test2(){
  return "method test2";
 }
}

在SpingMVC啟動(dòng)(初始化)階段檢查RequestMapping配置是否有歧義,這是其中一處檢查歧義的(后面還會(huì)提到一個(gè)在運(yùn)行時(shí)檢查歧義性的地方)。然后確認(rèn)配置正常以后會(huì)把該RequestMappingInfo和HandlerMethod對(duì)象添加至handlerMethods(LinkedHashMap)中,靜接著把RequestMapping注解的value和ReuqestMappingInfo對(duì)象添加至urlMap中。

registerHandlerMethod方法簡(jiǎn)單總結(jié)

該方法的主要有3個(gè)職責(zé)

1. 檢查RequestMapping注解配置是否有歧義。

2. 構(gòu)建RequestMappingInfo到HandlerMethod的映射map。該map便是AbstractHandlerMethodMapping的成員變量handlerMethods。LinkedHashMap。

3. 構(gòu)建AbstractHandlerMethodMapping的成員變量urlMap,MultiValueMap。這個(gè)數(shù)據(jù)結(jié)構(gòu)可以把它理解成Map>。其中String類型的key存放的是處理方法上RequestMapping注解的value。就是具體的uri

先有如下Controller

@Controller
@RequestMapping("/UrlMap")
public class UrlMapController {
 @RequestMapping(value = "/test1", method = RequestMethod.GET)
 @ResponseBody
 public String test1(){
  return "method test1";
 }

 @RequestMapping(value = "/test1")
 @ResponseBody
 public String test2(){
  return "method test2";
 }

 @RequestMapping(value = "/test3")
 @ResponseBody
 public String test3(){
  return "method test3";
 }
}

初始化完成后,對(duì)應(yīng)AbstractHandlerMethodMapping的urlMap的結(jié)構(gòu)如下

Spring MVC之Controller查找的示例分析

以上便是SpringMVC初始化的主要過(guò)程

查找過(guò)程

為了理解查找流程,帶著一個(gè)問(wèn)題來(lái)看,現(xiàn)有如下Controller

@Controller
@RequestMapping("/LookupTest")
public class LookupTestController {

 @RequestMapping(value = "/test1", method = RequestMethod.GET)
 @ResponseBody
 public String test1(){
  return "method test1";
 }

 @RequestMapping(value = "/test1", headers = "Referer=https://www.baidu.com")
 @ResponseBody
 public String test2(){
  return "method test2";
 }

 @RequestMapping(value = "/test1", params = "id=1")
 @ResponseBody
 public String test3(){
  return "method test3";
 }

 @RequestMapping(value = "/*")
 @ResponseBody
 public String test4(){
  return "method test4";
 }
}

有如下請(qǐng)求

Spring MVC之Controller查找的示例分析

這個(gè)請(qǐng)求會(huì)進(jìn)入哪一個(gè)方法?

web容器(Tomcat、jetty)接收請(qǐng)求后,交給DispatcherServlet處理。FrameworkServlet調(diào)用對(duì)應(yīng)請(qǐng)求方法(eg:get調(diào)用doGet),然后調(diào)用processRequest方法。進(jìn)入processRequest方法后,一系列處理后,在line:936進(jìn)入doService方法。然后在Line856進(jìn)入doDispatch方法。在line:896獲取當(dāng)前請(qǐng)求的處理器handler。然后進(jìn)入AbstractHandlerMethodMapping的lookupHandlerMethod方法。代碼如下

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
 List<Match> matches = new ArrayList<Match>();
 //根據(jù)uri獲取直接匹配的RequestMappingInfos
 List<T> directPathMatches = this.urlMap.get(lookupPath);
 if (directPathMatches != null) {
  addMatchingMappings(directPathMatches, matches, request);
 }
 //不存在直接匹配的RequetMappingInfo,遍歷所有RequestMappingInfo
 if (matches.isEmpty()) {
  // No choice but to go through all mappings
  addMatchingMappings(this.handlerMethods.keySet(), matches, request);
 }
 //獲取最佳匹配的RequestMappingInfo對(duì)應(yīng)的HandlerMethod
 if (!matches.isEmpty()) {
  Comparator<Match> comparator = new MatchComparator(getMappingComparator(request));
  Collections.sort(matches, comparator);

  if (logger.isTraceEnabled()) {
  logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);
  }
  //再一次檢查配置的歧義性
  Match bestMatch = matches.get(0);
  if (matches.size() > 1) {
  Match secondBestMatch = matches.get(1);
  if (comparator.compare(bestMatch, secondBestMatch) == 0) {
   Method m1 = bestMatch.handlerMethod.getMethod();
   Method m2 = secondBestMatch.handlerMethod.getMethod();
   throw new IllegalStateException(
     "Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" +
     m1 + ", " + m2 + "}");
  }
  }

  handleMatch(bestMatch.mapping, lookupPath, request);
  return bestMatch.handlerMethod;
 }
 else {
  return handleNoMatch(handlerMethods.keySet(), lookupPath, request);
 }
}

進(jìn)入lookupHandlerMethod方法,其中l(wèi)ookupPath="/LookupTest/test1",根據(jù)lookupPath,也就是請(qǐng)求的uri。直接查找urlMap,獲取直接匹配的RequestMappingInfo list。這里會(huì)匹配到3個(gè)RequestMappingInfo。如下

Spring MVC之Controller查找的示例分析

然后進(jìn)入addMatchingMappings方法

private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {
 for (T mapping : mappings) {
  T match = getMatchingMapping(mapping, request);
  if (match != null) {
  matches.add(new Match(match, handlerMethods.get(mapping)));
  }
 }
}

這個(gè)方法的職責(zé)是遍歷當(dāng)前請(qǐng)求的uri和mappings中的RequestMappingInfo能否匹配上,如果能匹配上,創(chuàng)建一個(gè)相同的RequestMappingInfo對(duì)象。再獲取RequestMappingInfo對(duì)應(yīng)的handlerMethod。然后創(chuàng)建一個(gè)Match對(duì)象添加至matches list中。執(zhí)行完addMatchingMappings方法,回到lookupHandlerMethod。這時(shí)候matches還有3個(gè)能匹配上的RequestMappingInfo對(duì)象。接下來(lái)的處理便是對(duì)matchers列表進(jìn)行排序,然后獲取列表的第一個(gè)元素作為最佳匹配。返回Match的HandlerMethod。這里進(jìn)入RequestMappingInfo的compareTo方法,看一下具體的排序邏輯。代碼如下

public int compareTo(RequestMappingInfo other, HttpServletRequest request) {
 int result = patternsCondition.compareTo(other.getPatternsCondition(), request);
 if (result != 0) {
  return result;
 }
 result = paramsCondition.compareTo(other.getParamsCondition(), request);
 if (result != 0) {
  return result;
 }
 result = headersCondition.compareTo(other.getHeadersCondition(), request);
 if (result != 0) {
  return result;
 }
 result = consumesCondition.compareTo(other.getConsumesCondition(), request);
 if (result != 0) {
  return result;
 }
 result = producesCondition.compareTo(other.getProducesCondition(), request);
 if (result != 0) {
  return result;
 }
 result = methodsCondition.compareTo(other.getMethodsCondition(), request);
 if (result != 0) {
  return result;
 }
 result = customConditionHolder.compareTo(other.customConditionHolder, request);
 if (result != 0) {
  return result;
 }
 return 0;
}

代碼里可以看出,匹配的先后順序是value>params>headers>consumes>produces>methods>custom,看到這里,前面的問(wèn)題就能輕易得出答案了。在value相同的情況,params更能先匹配。所以那個(gè)請(qǐng)求會(huì)進(jìn)入test3()方法。再回到lookupHandlerMethod,在找到HandlerMethod。SpringMVC還會(huì)這里再一次檢查配置的歧義性,這里檢查的原理是通過(guò)比較匹配度最高的兩個(gè)RequestMappingInfo進(jìn)行比較。此處可能會(huì)有疑問(wèn)在初始化SpringMVC有檢查配置的歧義性,這里為什么還會(huì)檢查一次。假如現(xiàn)在Controller中有如下兩個(gè)方法,以下配置是能通過(guò)初始化歧義性檢查的。

@RequestMapping(value = "/test5", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String test5(){
 return "method test5";
}
@RequestMapping(value = "/test5", method = {RequestMethod.GET, RequestMethod.DELETE})
@ResponseBody
public String test6(){
 return "method test6";
}

現(xiàn)在執(zhí)行 http://localhost:8080/SpringMVC-Demo/LookupTest/test5 請(qǐng)求,便會(huì)在lookupHandlerMethod方法中拋
java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/SpringMVC-Demo/LookupTest/test5'異常。這里拋該異常是因?yàn)镽equestMethodsRequestCondition的compareTo方法是比較的method數(shù)。代碼如下

public int compareTo(RequestMethodsRequestCondition other, HttpServletRequest request) {
 return other.methods.size() - this.methods.size();
}

什么時(shí)候匹配通配符?當(dāng)通過(guò)urlMap獲取不到直接匹配value的RequestMappingInfo時(shí)才會(huì)走通配符匹配進(jìn)入addMatchingMappings方法。

關(guān)于“Spring MVC之Controller查找的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

分享題目:SpringMVC之Controller查找的示例分析
標(biāo)題鏈接:http://muchs.cn/article38/jcjhsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、企業(yè)建站、外貿(mào)建站、云服務(wù)器、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)