关于go:马哥高端Go语言百万并发高薪班7期2022最新完结无密含文档源码

41次阅读

共计 4167 个字符,预计需要花费 11 分钟才能阅读完成。

download:马哥高端 Go 语言百万并发高薪班 7 期 -2022 最新完结无密含文档源码

spring MVC 源码探索之 AbstractHandlerMethodMapping
AbstractHandlerMethodMapping 是什么
解释是这样的。
/**

  • Abstract base class for {@link HandlerMapping} implementations that define
  • a mapping between a request and a {@link HandlerMethod}.
    *
  • <p>For each registered handler method, a unique mapping is maintained with
  • subclasses defining the details of the mapping type {@code <T>}.
  • @param <T> The mapping for a {@link HandlerMethod} containing the conditions
  • needed to match the handler method to incoming request.
    */

public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping implements InitializingBean
复制代码
我的理解为 AbstractHandlerMethodMapping 为每个注册的 handler method,对于每个子类映射类型都保护着其唯一的一个映射,就是保护 handler method 和 URL 的关系。次要用于 @Controller,@RequestMapping 等注解
AbstractHandlerMethodMapping 实现了 InitializingBean 接口,InitializingBean 是在程序启动的时候执行其唯一的 afterPropertiesSet() 方法,那咱们就先看一下启动时候的要做哪些操作。
initHandlerMethods()
/**

  在初始化的时候发现 handler methods
* Detects handler methods at initialization.
 */
@Override
public void afterPropertiesSet() {initHandlerMethods();
}

复制代码
在我的项目启动时会执行 initHandlerMethods 方法,它的次要功能是扫描利用上下文,发现并注册 handler methods。
/**

  • Scan beans in the ApplicationContext, detect and register handler methods.
  • @see #isHandler(Class)
  • @see #getMappingForMethod(Method, Class)
  • @see #handlerMethodsInitialized(Map)
    */

protected void initHandlerMethods() {

    if (logger.isDebugEnabled()) {logger.debug("Looking for request mappings in application context:" + getApplicationContext());
    }
    // 获取 IOC 容器中所有 bean
    String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
                    BeanFactoryUtils.beanNamesForTypeIncludingAncestors(obtainApplicationContext(), Object.class) :
                    obtainApplicationContext().getBeanNamesForType(Object.class));

    for (String beanName : beanNames) {if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
                    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.isDebugEnabled()) {logger.debug("Could not resolve target class for bean with name'" + beanName + "'", ex);
                            }
                    }
                    // 判断 bean 是否有 Controller 注解或者 RequestMapping 注解
                    if (beanType != null && isHandler(beanType)) {detectHandlerMethods(beanName);
                    }
            }
    }
    handlerMethodsInitialized(getHandlerMethods());

}
复制代码
isHandler()
AbstractHandlerMethodMapping#isHandler()
RequestMappingHandlerMapping 是目前为止 AbstractHandlerMethodMapping 的子类中唯一实现了 isHandler() 方法的子类,看下实现
protected boolean isHandler(Class<?> beanType) {

    return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
            AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}

复制代码
isHandler() 方法在这里的作用就是查看 beanType 是否有 @Controller 或 @RequestMapping 注解,这两个注解常常使用,应该都很熟悉了。
detectHandlerMethods()
AbstractHandlerMethodMapping#detectHandlerMethods() 这个方法的作用是从 handler 中获取 handler method 并注册
protected void detectHandlerMethods(final Object handler) {

    Class<?> handlerType = (handler instanceof String ?
                    obtainApplicationContext().getType((String) handler) : handler.getClass());

    if (handlerType != null) {
            // 为给定的类返回用户定义的类: 通常只是给定的类,如果是 cglib 生成的子类,则返回原始的类。final 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.isDebugEnabled()) {logger.debug(methods.size() + "request handler methods found on" + userType + ":" + methods);
            }

            // 为查找到的 handler method 进行注册
            methods.forEach((method, mapping) -> {Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
                    registerHandlerMethod(handler, invocableMethod, mapping);
            });
    }
}

复制代码
registerHandlerMethod()
AbstractHandlerMethodMapping#registerHandlerMethod() 的作用是为每个 handler method 注册它们唯一的映射路径,源码如下:
/**

  • Register a handler method and its unique mapping. Invoked at startup for
  • each detected handler method.
  • @param handler the bean name of the handler or the handler instance
  • @param method the method to register
  • @param mapping the mapping conditions associated with the handler method
  • @throws IllegalStateException if another method was already registered
  • under the same mapping
    */

protected void registerHandlerMethod(Object handler, Method method, T mapping) {

    this.mappingRegistry.register(mapping, handler, method);

}
复制代码
register 方法是 AbstractHandlerMethodMapping 外部类 MappingRegistry 里的方法,

正文完
 0