Class AbstractDispatcherServletInitializer
- java.lang.Object
- org.springframework.web.context.AbstractContextLoaderInitializer
- org.springframework.web.servlet.support.AbstractDispatcherServletInitializer
- All Implemented Interfaces:
WebApplicationInitializer
- Direct Known Subclasses:
AbstractAnnotationConfigDispatcherServletInitializer
public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer
Base class forWebApplicationInitializer
implementations that register aDispatcherServlet
in the servlet context.Concrete implementations are required to implement
createServletApplicationContext()
, as well asgetServletMappings()
, both of which get invoked fromregisterDispatcherServlet(ServletContext)
. Further customization can be achieved by overridingcustomizeRegistration(ServletRegistration.Dynamic)
.Because this class extends from
AbstractContextLoaderInitializer
, concrete implementations are also required to implementAbstractContextLoaderInitializer.createRootApplicationContext()
to set up a parent "root" application context. If a root context is not desired, implementations can simply returnnull
in thecreateRootApplicationContext()
implementation.- Since:
- 3.2
- Author:
- Arjen Poutsma, Chris Beams, Rossen Stoyanchev, Juergen Hoeller, Stephane Nicoll
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_SERVLET_NAME
The default servlet name.Fields inherited from class org.springframework.web.context.AbstractContextLoaderInitializer
logger
Constructor Summary
Constructors Constructor Description AbstractDispatcherServletInitializer()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected FrameworkServlet
createDispatcherServlet(WebApplicationContext servletAppContext)
Create aDispatcherServlet
(or other kind ofFrameworkServlet
-derived dispatcher) with the specifiedWebApplicationContext
.protected abstract WebApplicationContext
createServletApplicationContext()
Create a servlet application context to be provided to theDispatcherServlet
.protected void
customizeRegistration(ServletRegistration.Dynamic registration)
Optionally perform further registration customization onceregisterDispatcherServlet(ServletContext)
has completed.protected ApplicationContextInitializer<?>[]
getServletApplicationContextInitializers()
Specify application context initializers to be applied to the servlet-specific application context that theDispatcherServlet
is being created with.protected Filter[]
getServletFilters()
Specify filters to add and map to theDispatcherServlet
.protected abstract String[]
getServletMappings()
Specify the servlet mapping(s) for theDispatcherServlet
— for example"/"
,"/app"
, etc.protected String
getServletName()
Return the name under which theDispatcherServlet
will be registered.protected boolean
isAsyncSupported()
A single place to control theasyncSupported
flag for theDispatcherServlet
and all filters added viagetServletFilters()
.void
onStartup(ServletContext servletContext)
Configure the givenServletContext
with any servlets, filters, listeners context-params and attributes necessary for initializing this web application.protected void
registerDispatcherServlet(ServletContext servletContext)
Register aDispatcherServlet
against the given servlet context.protected FilterRegistration.Dynamic
registerServletFilter(ServletContext servletContext, Filter filter)
Add the given filter to the ServletContext and map it to theDispatcherServlet
as follows: a default filter name is chosen based on its concrete type theasyncSupported
flag is set depending on the return value ofasyncSupported
a filter mapping is created with dispatcher typesREQUEST
,FORWARD
,INCLUDE
, and conditionallyASYNC
depending on the return value ofasyncSupported
Methods inherited from class org.springframework.web.context.AbstractContextLoaderInitializer
createRootApplicationContext, getRootApplicationContextInitializers, registerContextLoaderListener
Field Detail
DEFAULT_SERVLET_NAME
public static final String DEFAULT_SERVLET_NAME
The default servlet name. Can be customized by overridinggetServletName()
.- See Also:
- Constant Field Values
Constructor Detail
AbstractDispatcherServletInitializer
public AbstractDispatcherServletInitializer()
Method Detail
onStartup
public void onStartup(ServletContext servletContext) throws ServletException
Description copied from interface:WebApplicationInitializer
Configure the givenServletContext
with any servlets, filters, listeners context-params and attributes necessary for initializing this web application. See examples above.- Specified by:
onStartup
in interfaceWebApplicationInitializer
- Overrides:
onStartup
in classAbstractContextLoaderInitializer
- Parameters:
servletContext
- theServletContext
to initialize- Throws:
ServletException
- if any call against the givenServletContext
throws aServletException
registerDispatcherServlet
protected void registerDispatcherServlet(ServletContext servletContext)
Register aDispatcherServlet
against the given servlet context.This method will create a
DispatcherServlet
with the name returned bygetServletName()
, initializing it with the application context returned fromcreateServletApplicationContext()
, and mapping it to the patterns returned fromgetServletMappings()
.Further customization can be achieved by overriding
customizeRegistration(ServletRegistration.Dynamic)
orcreateDispatcherServlet(WebApplicationContext)
.- Parameters:
servletContext
- the context to register the servlet against
getServletName
protected String getServletName()
Return the name under which theDispatcherServlet
will be registered. Defaults toDEFAULT_SERVLET_NAME
.
createServletApplicationContext
protected abstract WebApplicationContext createServletApplicationContext()
Create a servlet application context to be provided to theDispatcherServlet
.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.
createDispatcherServlet
protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext)
Create aDispatcherServlet
(or other kind ofFrameworkServlet
-derived dispatcher) with the specifiedWebApplicationContext
.Note: This allows for any
FrameworkServlet
subclass as of 4.2.3. Previously, it insisted on returning aDispatcherServlet
or subclass thereof.
getServletApplicationContextInitializers
protected ApplicationContextInitializer<?>[] getServletApplicationContextInitializers()
Specify application context initializers to be applied to the servlet-specific application context that theDispatcherServlet
is being created with.
getServletMappings
protected abstract String[] getServletMappings()
Specify the servlet mapping(s) for theDispatcherServlet
— for example"/"
,"/app"
, etc.
getServletFilters
protected Filter[] getServletFilters()
Specify filters to add and map to theDispatcherServlet
.- Returns:
- an array of filters or
null
- See Also:
registerServletFilter(ServletContext, Filter)
registerServletFilter
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter)
Add the given filter to the ServletContext and map it to theDispatcherServlet
as follows:- a default filter name is chosen based on its concrete type
- the
asyncSupported
flag is set depending on the return value ofasyncSupported
- a filter mapping is created with dispatcher types
REQUEST
,FORWARD
,INCLUDE
, and conditionallyASYNC
depending on the return value ofasyncSupported
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 withfilter
- the filter to be registered- Returns:
- the filter registration
isAsyncSupported
protected boolean isAsyncSupported()
A single place to control theasyncSupported
flag for theDispatcherServlet
and all filters added viagetServletFilters()
.The default value is "true".
customizeRegistration
protected void customizeRegistration(ServletRegistration.Dynamic registration)
Optionally perform further registration customization onceregisterDispatcherServlet(ServletContext)
has completed.- Parameters:
registration
- theDispatcherServlet
registration to be customized- See Also:
registerDispatcherServlet(ServletContext)