Initialization process of Spring MVC-01-DispatchServlet

Posted by artfuldrone on Thu, 06 Feb 2020 12:05:32 +0100

1, Initialization process of dispatcher Servlet

The dispatcher servlet inherits the HttpServletBean class, which contains the initialization method init(), whose code is as follows:

 * Map config parameters onto bean properties of this servlet, and
 * invoke subclass initialization.
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
public final void init() throws ServletException {
  // Set bean properties from init parameters.
  PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
  if (!pvs.isEmpty()) {
    try {
      BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
      ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
      bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
      bw.setPropertyValues(pvs, true);
    catch (BeansException ex) {
      if (logger.isErrorEnabled()) {
        logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
      throw ex;

  // Let subclasses do whatever initialization they like.

This method obtains the attribute parameters configured in web.xml and sets these attributes to DispatcherServlet. init() method also contains a template method, initServletBean(), which needs its subclass to implement.

As can be seen from the above figure, the subclass of HttpServletBean class is FrameworkServlet. Here, the template method initServletBean() is implemented, and the code is as follows:

 * Overridden method of {@link HttpServletBean}, invoked after any bean properties
 * have been set. Creates this servlet's WebApplicationContext.
protected final void initServletBean() throws ServletException {
  getServletContext().log("Initializing Spring " + getClass().getSimpleName() + " '" + getServletName() + "'");
  if (logger.isInfoEnabled()) {"Initializing Servlet '" + getServletName() + "'");
  long startTime = System.currentTimeMillis();

  try {
    this.webApplicationContext = initWebApplicationContext();
  catch (ServletException | RuntimeException ex) {
    logger.error("Context initialization failed", ex);
    throw ex;

  // Omit other codes


You can see that the core of the initServletBean() method is to initialize the WebApplicationContext object, that is, the initWebApplicationContext() method.

The FrameworkServlet.initWebApplicationContext() code is as follows:

 * Initialize and publish the WebApplicationContext for this servlet.
 * <p>Delegates to {@link #createWebApplicationContext} for actual creation
 * of the context. Can be overridden in subclasses.
 * @return the WebApplicationContext instance
 * @see #FrameworkServlet(WebApplicationContext)
 * @see #setContextClass
 * @see #setContextConfigLocation
protected WebApplicationContext initWebApplicationContext() {
  WebApplicationContext rootContext =
  WebApplicationContext wac = null;

  if (this.webApplicationContext != null) {
    // A context instance was injected at construction time -> use it
    wac = this.webApplicationContext;
    if (wac instanceof ConfigurableWebApplicationContext) {
      ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
      if (!cwac.isActive()) {
        // The context has not yet been refreshed -> provide services such as
        // setting the parent context, setting the application context id, etc
        if (cwac.getParent() == null) {
          // The context instance was injected without an explicit parent -> set
          // the root application context (if any; may be null) as the parent
  if (wac == null) {
    // No context instance was injected at construction time -> see if one
    // has been registered in the servlet context. If one exists, it is assumed
    // that the parent context (if any) has already been set and that the
    // user has performed any initialization such as setting the context id
    wac = findWebApplicationContext();
  if (wac == null) {
    // No context instance is defined for this servlet -> create a local one
    wac = createWebApplicationContext(rootContext);

  if (!this.refreshEventReceived) {
    // Either the context is not a ConfigurableApplicationContext with refresh
    // support or the context injected at construction time had already been
    // refreshed -> trigger initial onRefresh manually here.
    synchronized (this.onRefreshMonitor) {

  if (this.publishContext) {
    // Publish the context as a servlet context attribute.
    String attrName = getServletContextAttributeName();
    getServletContext().setAttribute(attrName, wac);

  return wac;

This method is relatively long, and the most important createWebApplicationContext() method will be analyzed here. The createWebApplicationContext() method is used to create the WebApplicationContext object. Enter the method and find that it calls the FrameworkServlet's overload method createWebApplicationContext(ApplicationContext parent). The code of the overload method is as follows:

 * Instantiate the WebApplicationContext for this servlet, either a default
 * {@link}
 * or a {@link #setContextClass custom context class}, if set.
 * <p>This implementation expects custom contexts to implement the
 * {@link org.springframework.web.context.ConfigurableWebApplicationContext}
 * interface. Can be overridden in subclasses.
 * <p>Do not forget to register this servlet instance as application listener on the
 * created context (for triggering its {@link #onRefresh callback}, and to call
 * {@link org.springframework.context.ConfigurableApplicationContext#refresh()}
 * before returning the context instance.
 * @param parent the parent ApplicationContext to use, or {@code null} if none
 * @return the WebApplicationContext for this servlet
 * @see
protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
  Class<?> contextClass = getContextClass();
  if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
    throw new ApplicationContextException(
        "Fatal initialization error in servlet with name '" + getServletName() +
        "': custom WebApplicationContext class [" + contextClass.getName() +
        "] is not of type ConfigurableWebApplicationContext");
  ConfigurableWebApplicationContext wac =
      (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

  String configLocation = getContextConfigLocation();
  if (configLocation != null) {

  return wac;

In this method, the Web context object, i.e. ConfigurableWebApplicationContext object, is created by reflection using the instantialeclass () method of BeanUtils class, and the contextConfigLocation element configured in web.xml is set to the ConfigurableWebApplicationContext object. Finally, execute the method configureandrefresh webapplicationcontext() to refresh the web application context

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
		if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
			// The application context id is still set to its original default value
			// -> assign a more useful id based on available information
			if (this.contextId != null) {
			else {
				// Generate default id...
				wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
						ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());

		wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

		// The wac environment's #initPropertySources will be called in any case when the context
		// is refreshed; do it eagerly here to ensure servlet property sources are in place for
		// use in any post-processing or initialization that occurs below prior to #refresh
		ConfigurableEnvironment env = wac.getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());


The ConfigurableWebApplicationContext.refresh() method in the last line of this method will enter into the initialization process of the IoC container. For details, please refer to the code analysis of the IoC container, which will not be covered here.


Return to the initWebApplicationContext() method of the FrameworkServlet. When the web application context object is created, the onRefresh() method will continue to execute.

if (!this.refreshEventReceived) {
    // Either the context is not a ConfigurableApplicationContext with refresh
	// support or the context injected at construction time had already been
    // refreshed -> trigger initial onRefresh manually here.
    synchronized (this.onRefreshMonitor) {

The onRefresh() method is implemented by the dispatcher servlet class:

 * This implementation calls {@link #initStrategies}.
protected void onRefresh(ApplicationContext context) {

There is only one line of code in the onRefresh() method. Call the initStrategies() method:

 * Initialize the strategy objects that this servlet uses.
 * <p>May be overridden in subclasses in order to initialize further strategy objects.
protected void initStrategies(ApplicationContext context) {
  // Initialize file upload processor
  // Initialize localization processor
  // Initialize theme processor
  // Initialize the processor mapping (used to save the RequestMapping and Method mapping relationship configured in the Controller)
  // Initialize processor adapter (used to dynamically match Method parameters, including class conversion and dynamic mapping)
  // Initialize exception handler
  // Initialize request to view name conversion
  // Initialize view resolver
  // Initialize flash map manager

The initStrategies() method of initialization policy will use the default configuration to initialize without specifying the personalized configuration file. The default configuration is located in

# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.








As can be seen from the default configuration policy of, each processor is configured with a default implementation, that is, the local resolver of the localization processor uses the AcceptHeaderLocaleResolver by default. The function of initializing various processors has been annotated in the initStrategies() method. You can enter the method to see the specific implementation.

Topics: Programming Attribute xml Spring