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

你可能感兴趣的:(Spring源码解析--DispatcherServlet初始化过程)