Class AbstractDispatcherServletInitializer

java.lang.Object
org.springframework.web.context.AbstractContextLoaderInitializer
org.springframework.web.servlet.support.AbstractDispatcherServletInitializer
All Implemented Interfaces:
org.springframework.web.WebApplicationInitializer
Direct Known Subclasses:
AbstractAnnotationConfigDispatcherServletInitializer

public abstract class AbstractDispatcherServletInitializer extends org.springframework.web.context.AbstractContextLoaderInitializer
Base class for WebApplicationInitializer implementations that register a DispatcherServlet in the servlet context.

Most applications should consider extending the Spring Java config subclass AbstractAnnotationConfigDispatcherServletInitializer.

Since:
3.2
Author:
Arjen Poutsma, Chris Beams, Rossen Stoyanchev, Juergen Hoeller, Stephane Nicoll
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default servlet name.

    Fields inherited from class org.springframework.web.context.AbstractContextLoaderInitializer

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    createDispatcherServlet(org.springframework.web.context.WebApplicationContext servletAppContext)
    Create a DispatcherServlet (or other kind of FrameworkServlet-derived dispatcher) with the specified WebApplicationContext.
    protected abstract org.springframework.web.context.WebApplicationContext
    Create a servlet application context to be provided to the DispatcherServlet.
    protected void
    customizeRegistration(jakarta.servlet.ServletRegistration.Dynamic registration)
    Optionally perform further registration customization once registerDispatcherServlet(ServletContext) has completed.
    protected org.springframework.context.ApplicationContextInitializer<?> @Nullable []
    Specify application context initializers to be applied to the servlet-specific application context that the DispatcherServlet is being created with.
    protected jakarta.servlet.Filter @Nullable []
    Specify filters to add and map to the DispatcherServlet.
    protected abstract String[]
    Specify the servlet mapping(s) for the DispatcherServlet — for example "/", "/app", etc.
    protected String
    Return the name under which the DispatcherServlet will be registered.
    protected boolean
    A single place to control the asyncSupported flag for the DispatcherServlet and all filters added via getServletFilters().
    void
    onStartup(jakarta.servlet.ServletContext servletContext)
     
    protected void
    registerDispatcherServlet(jakarta.servlet.ServletContext servletContext)
    Register a DispatcherServlet against the given servlet context.
    protected jakarta.servlet.FilterRegistration.Dynamic
    registerServletFilter(jakarta.servlet.ServletContext servletContext, jakarta.servlet.Filter filter)
    Add the given filter to the ServletContext and map it to the DispatcherServlet as follows: a default filter name is chosen based on its concrete type the asyncSupported flag is set depending on the return value of asyncSupported a filter mapping is created with dispatcher types REQUEST, FORWARD, INCLUDE, and conditionally ASYNC depending on the return value of asyncSupported

    Methods inherited from class org.springframework.web.context.AbstractContextLoaderInitializer

    createRootApplicationContext, getRootApplicationContextInitializers, registerContextLoaderListener

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • AbstractDispatcherServletInitializer

      public AbstractDispatcherServletInitializer()
  • Method Details

    • onStartup

      public void onStartup(jakarta.servlet.ServletContext servletContext) throws jakarta.servlet.ServletException
      Specified by:
      onStartup in interface org.springframework.web.WebApplicationInitializer
      Overrides:
      onStartup in class org.springframework.web.context.AbstractContextLoaderInitializer
      Throws:
      jakarta.servlet.ServletException
    • registerDispatcherServlet

      protected void registerDispatcherServlet(jakarta.servlet.ServletContext servletContext)
      Register a DispatcherServlet against the given servlet context.

      This method will create a DispatcherServlet with the name returned by getServletName(), initializing it with the application context returned from createServletApplicationContext(), and mapping it to the patterns returned from getServletMappings().

      Further customization can be achieved by overriding customizeRegistration(ServletRegistration.Dynamic) or createDispatcherServlet(WebApplicationContext).

      Parameters:
      servletContext - the context to register the servlet against
    • getServletName

      protected String getServletName()
      Return the name under which the DispatcherServlet will be registered. Defaults to DEFAULT_SERVLET_NAME.
      See Also:
    • createServletApplicationContext

      protected abstract org.springframework.web.context.WebApplicationContext createServletApplicationContext()
      Create a servlet application context to be provided to the DispatcherServlet.

      The returned context is delegated to Spring's DispatcherServlet(WebApplicationContext). As such, it typically contains controllers, view resolvers, locale resolvers, and other web-related beans.

      See Also:
    • createDispatcherServlet

      protected FrameworkServlet createDispatcherServlet(org.springframework.web.context.WebApplicationContext servletAppContext)
      Create a DispatcherServlet (or other kind of FrameworkServlet-derived dispatcher) with the specified WebApplicationContext.

      Note: This allows for any FrameworkServlet subclass as of 4.2.3. Previously, it insisted on returning a DispatcherServlet or subclass thereof.

    • getServletApplicationContextInitializers

      protected org.springframework.context.ApplicationContextInitializer<?> @Nullable [] getServletApplicationContextInitializers()
      Specify application context initializers to be applied to the servlet-specific application context that the DispatcherServlet is being created with.
      Since:
      4.2
      See Also:
    • getServletMappings

      protected abstract String[] getServletMappings()
      Specify the servlet mapping(s) for the DispatcherServlet — for example "/", "/app", etc.
      See Also:
    • getServletFilters

      protected jakarta.servlet.Filter @Nullable [] getServletFilters()
      Specify filters to add and map to the DispatcherServlet.
      Returns:
      an array of filters or null
      See Also:
    • registerServletFilter

      protected jakarta.servlet.FilterRegistration.Dynamic registerServletFilter(jakarta.servlet.ServletContext servletContext, jakarta.servlet.Filter filter)
      Add the given filter to the ServletContext and map it to the DispatcherServlet as follows:
      • a default filter name is chosen based on its concrete type
      • the asyncSupported flag is set depending on the return value of asyncSupported
      • a filter mapping is created with dispatcher types REQUEST, FORWARD, INCLUDE, and conditionally ASYNC depending on the return value of asyncSupported

      If the above defaults are not suitable or insufficient, override this method and register filters directly with the ServletContext.

      Parameters:
      servletContext - the servlet context to register filters with
      filter - the filter to be registered
      Returns:
      the filter registration
    • isAsyncSupported

      protected boolean isAsyncSupported()
      A single place to control the asyncSupported flag for the DispatcherServlet and all filters added via getServletFilters().

      The default value is "true".

    • customizeRegistration

      protected void customizeRegistration(jakarta.servlet.ServletRegistration.Dynamic registration)
      Optionally perform further registration customization once registerDispatcherServlet(ServletContext) has completed.
      Parameters:
      registration - the DispatcherServlet registration to be customized
      See Also: