Spring Boot处理@RequestMapping注解

SpringMVC路由映射解析

要了解Spring MVC是怎么把@RequestMapping注解的方法以及类关联到相应的url上,首先需要看下AbstractHandlerMethodMapping

public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping implements InitializingBean {

AbstractHandlerMethodMapping是一个InitializingBean,因此来看afterPropertiesSet()方法:

	@Override
	public void afterPropertiesSet() {
		initHandlerMethods();
	}
	protected void initHandlerMethods() {
        // …略
        // 读取Spring容器中的所有beanNames
		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) {
					// …略
				}
                // 判断这个bean是不是处理类
                // 根据这个类是否使用了@Controller或@RequestMapping
				if (beanType != null && isHandler(beanType)) {
					detectHandlerMethods(beanName);
				}
			}
		}
		handlerMethodsInitialized(getHandlerMethods());
	}

关注点就需要放在detectHandlerMethods这个方法上:

	protected void detectHandlerMethods(final Object handler) {
		Class<?> handlerType = (handler instanceof String ?
				getApplicationContext().getType((String) handler) : handler.getClass());
		final Class<?> userType = ClassUtils.getUserClass(handlerType);
 
        // 从处理类(@Controller)中获取处理方法(@RequestMapping)
		Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
				new MethodIntrospector.MetadataLookup<T>() {
					@Override
					public T inspect(Method method) {
						try {
                            // 返回RequestMappingInfo,包含@RequestMapping的标签信息
							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中:

		@Bean
		@Primary
		@Override
		public RequestMappingHandlerMapping requestMappingHandlerMapping() {
			// Must be @Primary for MvcUriComponentsBuilder to work
			return super.requestMappingHandlerMapping();
		}

定义一个requestMappingHandlerMapping的bean:

	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
		RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
		// …略
		return mapping;
	}

RequestMappingHandlerMapping定义如下:

public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
		implements MatchableHandlerMapping, EmbeddedValueResolverAware {
public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping<RequestMappingInfo> {

RequestMappingHandlerMapping继承了AbstractHandlerMethodMapping,就把AbstractHandlerMethodMapping注册到了Spring容器中。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值