SpringMVC—controller映射信息存储时机和过程

controller映射信息存储发生在Web容器初始化时,通过前面的博客我们知道,Web容器初始化时,会调用doCreateBean(),其中又会调用populateBean()和initializeBean(),而映射信息的注入发送在后者。

 

每一bean的创建初始化都会经过,我们只需要找到我们的controller,找到后,我们来看看initializeBean():

//最终我们找到了 我们要的那个bean
//RequestMappingHandlerMapping
 protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
        if (System.getSecurityManager() != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
                    return null;
                }
            }, this.getAccessControlContext());
        } else {
        //调用实现Aware接口的方法  BeanFactoryAware接口 主要方法
            this.invokeAwareMethods(beanName, bean);
        }

        Object wrappedBean = bean;
        if (mbd == null || !mbd.isSynthetic()) {
            //加强
            wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
        }

        try {
            //真正调用初始化方法
            this.invokeInitMethods(beanName, wrappedBean, mbd);
        } catch (Throwable var6) {
            throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
        }

        if (mbd == null || !mbd.isSynthetic()) {
            //使用后置处理器加强
            wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }

        return wrappedBean;
    }

因为我们使用@Controller标识,而controller又是RequestMappingHandlerMapping,它实现了Aware接口的类,所有会调用Aware的方法:invokeAwareMethods()调用完后才调用初始化方法:invokeInitMethods()

我们来看看invokeInitMethods():

可以发现它又会调用afterPropertiesSet()方法:

我们在来看看initHandlerMethods():

    protected void initHandlerMethods() {
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for request mappings in application context: " + getApplicationContext());
        }
        String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
                BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
                getApplicationContext().getBeanNamesForType(Object.class));
                //获取所有的bean
        for (String beanName : beanNames) {
            if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
                Class<?> beanType = null;
                try {
                    beanType = getApplicationContext().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);
                    }
                }
                //判断是不是Handler  controller
                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);
        //通过反射拿到多有方法 执行 getMappingForMethod  拿到分装了info的Map
        //key是Method value是RequestMappingInfo 
        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);
                        }
                    }
                });

        if (logger.isDebugEnabled()) {
            logger.debug(methods.size() + " request handler methods found on " + userType + ": " + methods);
        }
        for (Map.Entry<Method, T> entry : methods.entrySet()) {
            Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType);
            T mapping = entry.getValue();
            //将每一个映射RequestMappingInfo 注册到HandlerMethod
            registerHandlerMethod(handler, invocableMethod, mapping);
        }
 }

 以上就是一个完整的流程:

我们在细分的来看,首先是根据方法获取映射,而不是根据类,这点要注意,后面后说原因:

RequestMappingHandlerMapping.calss

        @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
    //拿到方法的映射信息
        RequestMappingInfo info = createRequestMappingInfo(method);
        if (info != null) {
        //拿到controller的映射
            RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
            if (typeInfo != null) {
            //组合 绑定
                info = typeInfo.combine(info);
            }
        }
        return info;
    }

          

      

首先是通过解析注解的方式拿到@RequestMapping标识的方法的信息的value,再拿到类的value,并不是我们以为的逻辑。

最后的组合,我们首先来看看RequestMappingInfo是什么:

        

接着我们来看看它是如何创建info的createRequestMappingInfo()方法:

然后我们看看,它是如何绑定的:

将key和value放到了patterns的Map中,和加入了很多其他信息,封装到RequestMappingInfo中

分装完后就是注入给容器:

它是注入到registry中,因此我们看看这是个什么:

可以看到registry是一个Map,值是一个MappingRegistration对象:

这样就注入到了AbstractHandlerMethodMapping类的属性中,而MappingRegistration中有一个handlerMethod,我们来看看:

分装的是bean信息,和method以及bridgedMethod

最后我们再看下类的关系结构:

 

请求发送后,如何获取?

请求处理流程

关注微信公众号,某课资源、技术精讲、妹纸免费看

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wonder4work

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值