要了解Spring MVC是怎么把@RequestMapping注解的方法以及类关联到相应的url上,首先需要看下AbstractHandlerMethodMapping:
1 public abstract class AbstractHandlerMethodMapping <T> extends AbstractHandlerMapping implements InitializingBean {
AbstractHandlerMethodMapping是一个InitializingBean,因此来看afterPropertiesSet()方法:
1 2 3 4 @Override public void afterPropertiesSet () { initHandlerMethods(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 protected void initHandlerMethods () { String[] beanNames = (this .detectHandlerMethodsInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) : getApplicationContext().getBeanNamesForType(Object.class)); for (String beanName : beanNames) { if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) { Class<?> beanType = null ; try { beanType = getApplicationContext().getType(beanName); } catch (Throwable ex) { } if (beanType != null && isHandler(beanType)) { detectHandlerMethods(beanName); } } } handlerMethodsInitialized(getHandlerMethods()); }
关注点就需要放在detectHandlerMethods这个方法上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 protected void detectHandlerMethods (final Object handler) { Class<?> handlerType = (handler instanceof String ? getApplicationContext().getType((String) handler) : handler.getClass()); final Class<?> userType = ClassUtils.getUserClass(handlerType); Map<Method, T> methods = MethodIntrospector.selectMethods(userType, new MethodIntrospector .MetadataLookup<T>() { @Override public T inspect (Method method) { try { return getMappingForMethod(method, userType); } catch (Throwable ex) { throw new IllegalStateException ("Invalid mapping on handler class [" + userType.getName() + "]: " + method, ex); } } }); for (Map.Entry<Method, T> entry : methods.entrySet()) { Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType); T mapping = entry.getValue(); registerHandlerMethod(handler, invocableMethod, mapping); } }
先调用getMappingForMethod获取RequestMappingInfo,然后调用registerHandlerMethod注册到MappingRegistry中,在MappingRegistry中关联了url与HandlerMethod、url与RequestMappingInfo等。
到这里,知道了AbstractHandlerMethodMapping怎么处理了@RequestMapping,那么AbstractHandlerMethodMapping是什么时候加入到了Spring容器中的呢?答案是实例化RequestMappingHandlerMapping。 Spring Boot的自动注解中,由ConfigurationClassPostProcessor把在配置项中的WebMvcAutoConfiguration进行处理,会把WebMvcAutoConfiguration$EnableWebMvcConfiguration注册到beanFactory中, 在EnableWebMvcConfiguration中:
1 2 3 4 5 6 7 @Bean @Primary @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { // Must be @Primary for MvcUriComponentsBuilder to work return super.requestMappingHandlerMapping(); }
定义一个requestMappingHandlerMapping的bean:
1 2 3 4 5 public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping(); // …略 return mapping; }
在RequestMappingHandlerMapping定义如下:
1 2 public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping implements MatchableHandlerMapping, EmbeddedValueResolverAware {
1 public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping<RequestMappingInfo> {
在RequestMappingHandlerMapping继承了AbstractHandlerMethodMapping,就把AbstractHandlerMethodMapping注册到了Spring容器中。