Spring源码解析--DispatcherServlet初始化过程

1. DispatcherServlet

  protected void onRefresh(ApplicationContext context)
  {
    initStrategies(context);
  }
  
  protected void initStrategies(ApplicationContext context)
  {
    initMultipartResolver(context);
    initLocaleResolver(context);
    initThemeResolver(context);
    initHandlerMappings(context);
    initHandlerAdapters(context);
    initHandlerExceptionResolvers(context);
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);
    initFlashMapManager(context);
  }

2. FrameworkServlet

  protected final void initServletBean()
    throws ServletException
  {
    getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
    if (this.logger.isInfoEnabled()) {
      this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
    }
    long startTime = System.currentTimeMillis();
    try
    {
      this.webApplicationContext = initWebApplicationContext();
      initFrameworkServlet();
    }
    catch (ServletException ex)
    {
      this.logger.error("Context initialization failed", ex);
      throw ex;
    }
    catch (RuntimeException ex)
    {
      this.logger.error("Context initialization failed", ex);
      throw ex;
    }
    if (this.logger.isInfoEnabled())
    {
      long elapsedTime = System.currentTimeMillis() - startTime;
      this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " + elapsedTime + " ms");
    }
  }

initWebApplicationContext 

  protected WebApplicationContext initWebApplicationContext()
  {
    WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    WebApplicationContext wac = null;
    if (this.webApplicationContext != null)
    {
      wac = this.webApplicationContext;
      if ((wac instanceof ConfigurableWebApplicationContext))
      {
        ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)wac;
        if (!cwac.isActive())
        {
          if (cwac.getParent() == null) {
            cwac.setParent(rootContext);
          }
          configureAndRefreshWebApplicationContext(cwac);
        }
      }
    }
    if (wac == null) {
      wac = findWebApplicationContext();
    }
    if (wac == null) {
      wac = createWebApplicationContext(rootContext);
    }
    if (!this.refreshEventReceived) {
      onRefresh(wac);
    }
    if (this.publishContext)
    {
      String attrName = getServletContextAttributeName();
      getServletContext().setAttribute(attrName, wac);
      if (this.logger.isDebugEnabled()) {
        this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() + "' as ServletContext attribute with name [" + attrName + "]");
      }
    }
    return wac;
  }

configureRefreshWebApplicationContext:

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac)
  {
    if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
      if (this.contextId != null) {
        wac.setId(this.contextId);
      } else {
        wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + 
          ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
      }
    }
    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getNamespace());
    wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener(null)));
    
    ConfigurableEnvironment env = wac.getEnvironment();
    if ((env instanceof ConfigurableWebEnvironment)) {
      ((ConfigurableWebEnvironment)env).initPropertySources(getServletContext(), getServletConfig());
    }
    postProcessWebApplicationContext(wac);
    applyInitializers(wac);
    wac.refresh();
  }

3. HttpServletBean

  public final void init()
    throws ServletException
  {
    if (this.logger.isDebugEnabled()) {
      this.logger.debug("Initializing servlet '" + getServletName() + "'");
    }
    try
    {
      PropertyValues pvs = 
              new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
      BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
      ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
      bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
      initBeanWrapper(bw);
      bw.setPropertyValues(pvs, true);
    }
    catch (BeansException ex)
    {
      this.logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
      throw ex;
    }
    initServletBean();
    if (this.logger.isDebugEnabled()) {
      this.logger.debug("Servlet '" + getServletName() + "' configured successfully");
    }
  }

 

转载于:https://my.oschina.net/u/3896435/blog/1841294

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值