在Spring MVC中,定义了多种URL与Controller映射关系的描述方式。在基于注解的Spring MVC中,采用Java注解的方式描述URL与Controller之间的关系,那么Spring MVC是如何获取这些映射关系,并将其注册到handlerMap中呢?这些问题将是本文研究的重点。
Spring MVC使用HandlerMapping接口抽象表示通过请求获取Controller的行为,在使用注解驱动的Spring MVC中,HandlerMapping的具体实现类为:DefaultAnnotationHandlerMapping,该类继承自AbstractDetectingHandlerMapping,在AbstractDetectingHandlerMapping类中,定义了方法detectHandlers(),这个方法的目的在于取得所有可能的Controller,并将URL与Controller的映射关系注册到handlerMap中。首先开一下这个方法的代码实现。
//Register all handlers found in the current ApplicationContext. protected void detectHandlers() throws BeansException { if (logger.isDebugEnabled()) { logger.debug(“Looking for URL mappings in application context: ” + getApplicationContext()); } //取得容器中的搜有bean String[] beanNames = (this.detectHandlersInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) : getApplicationContext().getBeanNamesForType(Object.class)); // Take any bean name that we can determine URLs for. for (String beanName : beanNames) { //取得每个bean可以处理的url String[] urls = determineUrlsForHandler(beanName); if (!ObjectUtils.isEmpty(urls)) { // URL paths found: Let’s consider it a handler. //注册,将url与controller的映射关系注册到handlerMap中 registerHandler(urls, beanName); } else { if (logger.isDebugEnabled()) { logger.debug(“Rejected bean name ‘” + beanName + “‘: no URL paths identified”); } } } }
在AbstractDetectingHandlerMapping中,determineUrlsForHandler(String beanName)是一个抽象方法,由具体的子类给出实现,这里我们需要关注的是DefaultAnnotationHandlerMapping类是如何实现该方法的。代码如下:
protected String[] determineUrlsForHandler(String beanName) { ApplicationContext context = getApplicationContext(); Class<?> handlerType = context.getType(beanName); //取得该bean类级别的RequestMapping注解 RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class); if (mapping != null) { // @RequestMapping found at type level this.cachedMappings.put(handlerType, mapping); Set<String> urls = new LinkedHashSet<String>(); String[] typeLevelPatterns = mapping.value(); if (typeLevelPatterns.length > 0) { // @RequestMapping specifies paths at type level //获取方法中RequestMapping中定义的URL。(RequestMapping可以定义在类上,也可以定义在方法上) String[] methodLevelPatterns = determineUrlsForHandlerMethods(handlerType); for (String typeLevelPattern : typeLevelPatterns) { if (!typeLevelPattern.startsWith(“/”)) { typeLevelPattern = “/” + typeLevelPattern; } //将类级别定义的URL与方法级别定义的URL合并(合并规则后面再详解),合并后添加到该bean可以处理的URL集合中 for (String methodLevelPattern : methodLevelPatterns) { String combinedPattern = getPathMatcher().combine(typeLevelPattern, methodLevelPattern); addUrlsForPath(urls, combinedPattern); } //将类级别定义的URL添加到该bean可以处理的URL集合中 addUrlsForPath(urls, typeLevelPattern); } return StringUtils.toStringArray(urls); } else { // actual paths specified by @RequestMapping at method level //如果类级别的RequestMapping没有指定URL,则返回方法中RequestMapping定义的URL return determineUrlsForHandlerMethods(handlerType); } } else if (AnnotationUtils.findAnnotation(handlerType, Controller.class) != null) { // @RequestMapping to be introspected at method level //如果类级别没有定义RequestMapping,但是定义了Controller注解,将返回方法中RequestMapping定义的URL return determineUrlsForHandlerMethods(handlerType); } else { //类级别即没有定义RequestMapping,也没有定义Controller,则返回null return null; } }
上述代码是Spring处理类级别的RequestMapping注解,但是RequestMapping注解也可以定义在方法级别上,determineUrlsForHandlerMethods()方法是获取该类中定义了RequestMapping注解的方法能够处理的所有URL。下面看一下该方法的实现。
protected String[] determineUrlsForHandlerMethods(Class<?> handlerType) { final Set<String> urls = new LinkedHashSet<String>(); //类型有可能是代理类,如果是代理类,则取得它的所有接口 Class<?>[] handlerTypes = Proxy.isProxyClass(handlerType) ? handlerType.getInterfaces() : new Class<?>[]{handlerType}; for (Class<?> currentHandlerType : handlerTypes){ //依次处理该类的所有方法 ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() { public void doWith(Method method) { //取得方法界别的RequestMapping RequestMapping mapping = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (mapping != null) { //获取可以处理的URL String[] mappedPaths = mapping.value(); //将这些URL放入到可处理的URL集合中 for (String mappedPath : mappedPaths) { addUrlsForPath(urls, mappedPath); } } } }); } return StringUtils.toStringArray(urls); }
分别获取了类和方法级别的RequestMapping中定义的URL后,基本上完成了URL的提取工作,但是有一种情况需要处理:类和方法中同时定义了URL,这两个URL是如何合并的呢?规则又是怎样的呢?看一下URL合并代码:
public String combine(String pattern1, String pattern2) { if (!StringUtils.hasText(pattern1) && !StringUtils.hasText(pattern2)) { //如果两个URL都为空,那么返回空 return “”; } else if (!StringUtils.hasText(pattern1)) { //如果第一个为空,返回第二个 return pattern2; } else if (!StringUtils.hasText(pattern2)) { //如果第二个为空,则返回第一个 return pattern1; } else if (match(pattern1, pattern2)) { //如果两个URL匹配,则返回第二个 return pattern2; } else if (pattern1.endsWith(“/*”)) { if (pattern2.startsWith(“/”)) { // /hotels/* + /booking -> /hotels/booking return pattern1.substring(0, pattern1.length() – 1) + pattern2.substring(1); } else { // /hotels/* + booking -> /hotels/booking return pattern1.substring(0, pattern1.length() – 1) + pattern2; } } else if (pattern1.endsWith(“/**”)) { if (pattern2.startsWith(“/”)) { // /hotels/** + /booking -> /hotels/**/booking return pattern1 + pattern2; } else { // /hotels/** + booking -> /hotels/**/booking return pattern1 + “/” + pattern2; } } else { int dotPos1 = pattern1.indexOf(‘.’); if (dotPos1 == -1) { // simply concatenate the two patterns if (pattern1.endsWith(“/”) || pattern2.startsWith(“/”)) { return pattern1 + pattern2; } else { return pattern1 + “/” + pattern2; } } String fileName1 = pattern1.substring(0, dotPos1); String extension1 = pattern1.substring(dotPos1); String fileName2; String extension2; int dotPos2 = pattern2.indexOf(‘.’); if (dotPos2 != -1) { fileName2 = pattern2.substring(0, dotPos2); extension2 = pattern2.substring(dotPos2); } else { fileName2 = pattern2; extension2 = “”; } String fileName = fileName1.endsWith(“*”) ? fileName2 : fileName1; String extension = extension1.startsWith(“*”) ? extension2 : extension1; return fileName + extension; } }
通过以上的处理,基本上完成了bean可以处理的URL信息的提取,在代码中有个方法经常出现:addUrlsForPath(),该方法的目的是将RequestMapping中定义的path添加的URL集合中,如果指定PATH不是以默认的方式结尾,那么Spring将默认的结尾添加到该path上,并将处理结果添加到url集合中。
protected void addUrlsForPath(Set<String> urls, String path) { urls.add(path); if (this.useDefaultSuffixPattern && path.indexOf(‘.’) == -1 && !path.endsWith(“/”)) { urls.add(path + “.*”); urls.add(path + “/”); } }