Spring MVC流程

初始化阶段

首先找到 DispatcherServlet 这个类,必然是寻找 init()方法。然后,我们发现其 init 方法其实在父类 HttpServletBean 中又 调 用 了 一 个 重 要 的 initServletBean() 方 法最后有调用了

@Override

protected void onRefresh(ApplicationContext context) {

       initStrategies(context);//初始化九大组件

}

HandlerMapping 的 子 类 AbstractDetectingUrlHandlerMapping 实现了 initApplicationContext()方法

   /**
     * 建立当前 ApplicationContext 中的所有 Controller 和 url 的对应关系
     */
    protected void detectHandlers() throws BeansException {
        ApplicationContext applicationContext = obtainApplicationContext();
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for URL mappings in application context: " + applicationContext);
        }
// 获取 ApplicationContext 容器中所有 bean 的 Name
        String[] beanNames = (this.detectHandlersInAncestorContexts ?
                BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class) :
                applicationContext.getBeanNamesForType(Object.class));
// 遍历 beanNames,并找到这些 bean 对应的 url
        for (String beanName : beanNames) {
// 找 bean 上的所有 url(Controller 上的 url+方法上的 url),该方法由对应的子类实现
            String[] urls = determineUrlsForHandler(beanName);
            if (!ObjectUtils.isEmpty(urls)) {
// 保存 urls 和 beanName 的对应关系,put it to Map<urls,beanName>,
// 该方法在父类 AbstractUrlHandlerMapping 中实现
                registerHandler(urls, beanName);
            }
            else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Rejected bean name '" + beanName + "': no URL paths identified");
                }
            }
        }
    }
    /** 获取 Controller 中所有方法的 url,由子类实现,典型的模板模式 **/
    protected abstract String[] determineUrlsForHandler(String beanName);

 

运行调用阶段 

这一步步是由请求触发的,所以入口为 DispatcherServlet 的核心方法为 doService(), doService()中的核心逻辑由 doDispatch()实现,控制请求的转发

getHandler取得处理当前请求的 Controller,这里也称为 hanlder处理器。handleInternal()拼接 Controller 的 url 和方法的 url,与 Request 的 url 进行匹配,找到匹配的方法

Spring MVC 核心组件的关联关系

 springmvc时序图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值