spring requestMapping注解将url映射到controller方法

1url 映射的handler(即controller) 会放在RequestMappingHandlerMapping 类中,
RequestMappingHandlerMapping的父类是AbstractHandlerMethodMapping
AbstractHandlerMethodMapping 实现了InitializingBean

  1. RequestMappingHandlerMapping 覆盖了父类AbstractHandlerMethodMapping 的afterPropertiesSet 方法
@Override
	public void afterPropertiesSet() {
		this.config = new RequestMappingInfo.BuilderConfiguration();
		this.config.setUrlPathHelper(getUrlPathHelper());
		this.config.setPathMatcher(getPathMatcher());
		this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
		this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
		this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
		this.config.setContentNegotiationManager(getContentNegotiationManager());
         
         // 调用父类的afterPropertiesSet  方法
		super.afterPropertiesSet();
	}

AbstractHandlerMethodMapping 的afterPropertiesSet 方法

@Override
	public void afterPropertiesSet() {
		initHandlerMethods();
	}
  1. 初始化handler 方法
	protected void initHandlerMethods() {
	    // 遍历所有bean
		for (String beanName : getCandidateBeanNames()) {
			if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
			    // 处理候选bean
				processCandidateBean(beanName);
			}
		}
		handlerMethodsInitialized(getHandlerMethods());
	}
  1. 处理候选bean
protected void processCandidateBean(String beanName) {
		Class<?> beanType = null;
		try {
			beanType = obtainApplicationContext().getType(beanName);
		}
		catch (Throwable ex) {
			// An unresolvable bean type, probably from a lazy bean - let's ignore it.
			if (logger.isTraceEnabled()) {
				logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
			}
		}
		if (beanType != null && isHandler(beanType)) {
		    // 检测handler 方法(即controller的method)
			detectHandlerMethods(beanName);
		}
	}
  1. 检测handler 方法(即controller的method)
protected void detectHandlerMethods(Object handler) {
		Class<?> handlerType = (handler instanceof String ?
				obtainApplicationContext().getType((String) handler) : handler.getClass());

		if (handlerType != null) {
			Class<?> userType = ClassUtils.getUserClass(handlerType);
			Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
					(MethodIntrospector.MetadataLookup<T>) method -> {
						try {
							return getMappingForMethod(method, userType);
						}
						catch (Throwable ex) {
							throw new IllegalStateException("Invalid mapping on handler class [" +
									userType.getName() + "]: " + method, ex);
						}
					});
			if (logger.isTraceEnabled()) {
				logger.trace(formatMappings(userType, methods));
			}
			// method  对应controller的方法;   mapping 对应请求的url
			methods.forEach((method, mapping) -> {
				Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
				// 注册handler 方法
				registerHandlerMethod(handler, invocableMethod, mapping);
			});
		}
	}
  1. 注册handler 方法
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
		this.mappingRegistry.register(mapping, handler, method);
	}

看图就知道映射的url 和方法了
在这里插入图片描述

url 请求过来的时候就会走如下路径去获取 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#mappingRegistry 中的 handler方法

org.springframework.web.servlet.DispatcherServlet#doDispatch
org.springframework.web.servlet.DispatcherServlet#getHandler
org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandler
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal

点击查看参考博客, 写的也不错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值