DispatcherServlet
is the gateway to any Spring MVC application. Inherited from javax.servlet.http.HttpServlet
, it is typically configured declaratively in the web.xml
file. While you can have multiple definitions of DispatcherServlet
with unique URL patterns, most Spring MVC applications only have single DispatcherServlet
with the context-root
URL(/
), that is, all requests coming to that domain will be handled by DispatcherServlet
.
Starting from Servlet 3.0, in addition to declarative configuration in the web.xml
file, DispatcherServlet
can be configured programmatically by implementing or extending either of these three support classes provided by Spring:
WebAppInitializer
interfaceAbstractDispatcherServletInitializer
abstract classAbstractAnnotationConfigDispatcherServletInitializer
abstract classThe following code listing demonstrates how to implement a WebAppInitializer
directly in your application:
public class ApplicationInitializer implements WebApplicationInitializer { private static final Logger logger = LoggerFactory.getLogger(ApplicationInitializer.class); @Override public void onStartup(ServletContext servletContext) throws ServletException { logger.info("===== Application is starting up! ========"); XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB- INF/spring/appServlet/servlet- context.xml"); ServletRegistration.Dynamic registration = servletContext.addServlet("rootDispatcher", new DispatcherServlet(appContext)); registration.setLoadOnStartup(1); registration.addMapping("/"); }
DispatcherServlet
uses a specialized ApplicationContext
called WebApplicationContext
that has many web request processing capabilities. It is aware of which ServletContext
it is associated with and is also capable of resolving themes. This interface has concrete implementations for specific contexts such as XML, @Configuration
annotated classes, and portlets. By default, DispatcherServlet
uses XMLWebApplicationContext
. When DispatcherServlet
is loaded, it looks for the bean configuration file of WebApplicationContext
and initializes it.
WebApplicationContext
objects are hierarchical. Every Spring MVC application has root ApplicationContext
(configurable with a context-param
tag called contextConfigLocation
in the web.xml
file), and each Servlet, including DispatcherServlet
, has its own child context (configurable by its own init-param
, contextConfigLocation
). Ideally, Servlet-specific child contexts have beans customizing that Servlet, and root ApplicationContext
has all shared beans.
Upon receiving a web request, DispatcherServlet
performs a set of operations in sequence as part of the request processing, with the help of a set of supporting beans. This table lists these special beans and their responsibilities:
DispatcherServlet
is extremely flexible; we can even create and configure custom implementations for all these beans. However, Spring MVC provides a set of nice implementations by default so that you don't need to customize or provide your own implementations unless absolutely required. These default implementations can be found inside org.springframework.web.servlet.DispatcherServlet.properties
. If you override them with your own implementation of any of these beans, yours will override the defaults.