Spring MVC

The Spring Container

The Spring container can be instantiated in different ways:

  • Using Spring Core module

    We have to create an instance of a sub class of the ApplicationContext interface. We can user the ClassPathXmlApplicationContext implementation.

  • Using the Spring MVC module

    It is sufficient to load the ApplicationServlet and provide an XML configuration file. Automatically the Spring Container will be instantiated.

The WebApplicationContext

The WebApplicationContext interface is an extension to the ApplicationContext interface.

It only adds one extra method called getServletContext() and declare some constants values.

The WebApplicationContext initially loads three classes:

  • HandlerMapping class(es)
  • ViewResolver class(es)
  • Controller class(es)

DispatcherServlet

DispatcherServlet is a plain ordinary Servlet that belongs to the Servlet hierarchy: we need to declare the servlet in the web.xml.

Doing so, we are implicitly telling the Spring Container which configuration file to use (that is, servlet_name-servlet.xml).

Looking at org.springframework.web.servlet.DispatcherServlet API notice this:

“A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.”

This brings an interesting point – in a Spring Web App you have one root application context which is private, and many dispatcher servlet application contexts which are children of the root application context.