Top Description Fields Constructors Methods
org.springframework.web.servlet.support

public abstract Class AbstractDispatcherServletInitializer

extends AbstractContextLoaderInitializer
Class Inheritance
Known Direct Subclasses
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
Imports
java.util.EnumSet, jakarta.servlet.DispatcherType, .Filter, .FilterRegistration, .FilterRegistration.Dynamic, .ServletContext, .ServletException, .ServletRegistration, org.springframework.context.ApplicationContextInitializer, org.springframework.core.Conventions, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils, .StringUtils, org.springframework.web.context.AbstractContextLoaderInitializer, .WebApplicationContext, org.springframework.web.servlet.DispatcherServlet, .FrameworkServlet

Base class for org.springframework.web.WebApplicationInitializer implementations that register a DispatcherServlet in the servlet context.

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

Authors
Arjen Poutsma, Chris Beams, Rossen Stoyanchev, Juergen Hoeller, Stephane Nicoll
Since
3.2

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_SERVLET_NAME

The default servlet name.

Inherited from org.springframework.web.context.AbstractContextLoaderInitializer:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected FrameworkServlet
createDispatcherServlet(WebApplicationContext servletAppContext)

Create a DispatcherServlet (or other kind of FrameworkServlet-derived dispatcher) with the specified WebApplicationContext.

protected abstract WebApplicationContext
createServletApplicationContext()

Create a servlet application context to be provided to the DispatcherServlet.

protected void
customizeRegistration(ServletRegistration.Dynamic
the DispatcherServlet registration to be customized
registration
)

Optionally perform further registration customization once registerDispatcherServlet(ServletContext) has completed.

private EnumSet<DispatcherType>
protected ApplicationContextInitializer<?>[]
getServletApplicationContextInitializers()

Specify application context initializers to be applied to the servlet-specific application context that the DispatcherServlet is being created with.

protected Filter[]

Returns:

an array of filters or null
getServletFilters
()

Specify filters to add and map to the DispatcherServlet.

protected abstract String[]
getServletMappings()

Specify the servlet mapping(s) for the DispatcherServlet — for example "/", "/app", etc.

protected String
getServletName()

Return the name under which the DispatcherServlet will be registered.

protected boolean
isAsyncSupported()

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

public void
onStartup(ServletContext
the ServletContext to initialize
servletContext
)

Overrides org.springframework.web.context.AbstractContextLoaderInitializer.onStartup.

Implements org.springframework.web.WebApplicationInitializer.onStartup.

Configure the given ServletContext with any servlets, filters, listeners context-params and attributes necessary for initializing this web application.

protected void
registerDispatcherServlet(ServletContext
the context to register the servlet against
servletContext
)

Register a DispatcherServlet against the given servlet context.

protected FilterRegistration.Dynamic

Returns:

the filter registration
registerServletFilter
(ServletContext
the servlet context to register filters with
servletContext
,
Filter
the filter to be registered
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
Inherited from org.springframework.web.context.AbstractContextLoaderInitializer:
createRootApplicationContextgetRootApplicationContextInitializersregisterContextLoaderListener

Field Detail

DEFAULT_SERVLET_NAMEback to summary
public static final String DEFAULT_SERVLET_NAME

The default servlet name. Can be customized by overriding getServletName.

Constructor Detail

AbstractDispatcherServletInitializerback to summary
public AbstractDispatcherServletInitializer()

Method Detail

createDispatcherServletback to summary
protected FrameworkServlet createDispatcherServlet(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.

createServletApplicationContextback to summary
protected abstract WebApplicationContext createServletApplicationContext()

Create a servlet application context to be provided to the DispatcherServlet.

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

See Also
registerDispatcherServlet(ServletContext)
customizeRegistrationback to summary
protected void customizeRegistration(ServletRegistration.Dynamic registration)

Optionally perform further registration customization once registerDispatcherServlet(ServletContext) has completed.

Parameters
registration:ServletRegistration.Dynamic

the DispatcherServlet registration to be customized

See Also
registerDispatcherServlet(ServletContext)
getDispatcherTypesback to summary
private EnumSet<DispatcherType> getDispatcherTypes()
getServletApplicationContextInitializersback to summary
protected ApplicationContextInitializer<?>[] getServletApplicationContextInitializers()

Specify application context initializers to be applied to the servlet-specific application context that the DispatcherServlet is being created with.

Annotations
@Nullable
Since
4.2
See Also
createServletApplicationContext(), DispatcherServlet#setContextInitializers, getRootApplicationContextInitializers()
getServletFiltersback to summary
protected Filter[] getServletFilters()

Specify filters to add and map to the DispatcherServlet.

Returns:Filter[]

an array of filters or null

Annotations
@Nullable
See Also
registerServletFilter(ServletContext, Filter)
getServletMappingsback to summary
protected abstract String[] getServletMappings()

Specify the servlet mapping(s) for the DispatcherServlet — for example "/", "/app", etc.

See Also
registerDispatcherServlet(ServletContext)
getServletNameback to summary
protected String getServletName()

Return the name under which the DispatcherServlet will be registered. Defaults to DEFAULT_SERVLET_NAME.

See Also
registerDispatcherServlet(ServletContext)
isAsyncSupportedback to summary
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".

onStartupback to summary
public void onStartup(ServletContext servletContext) throws ServletException

Overrides org.springframework.web.context.AbstractContextLoaderInitializer.onStartup.

Implements org.springframework.web.WebApplicationInitializer.onStartup.

Doc from org.springframework.web.WebApplicationInitializer.onStartup.

Configure the given ServletContext with any servlets, filters, listeners context-params and attributes necessary for initializing this web application. See examples above.

Parameters
servletContext:ServletContext

the ServletContext to initialize

Annotations
@Override
Exceptions
ServletException:
if any call against the given ServletContext throws a ServletException
registerDispatcherServletback to summary
protected void registerDispatcherServlet(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:ServletContext

the context to register the servlet against

registerServletFilterback to summary
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, 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:ServletContext

the servlet context to register filters with

filter:Filter

the filter to be registered

Returns:FilterRegistration.Dynamic

the filter registration