Whats the difference between DispatcherServlet and ContextLoaderListener

Both ContextLoaderListener and DispatcherServlet create and maintain application context, an application context maintain a set of beans that serves theirs purpose.

The contents of application context are different

The context of DispatcherServlet contains MVC related beans, such as controllers, view resolves, handlers, they are only visible for MVC.

The context of ContextLoaderListener contains beans that globally visible, like services, repositories, infrastructure beans, etc. Root context is also used by other components, for example, the Struts MVC, Spring Security, any component want to utilize the Spring bean container need the root context.

Relationships between contexts

The root context is the parent, all DispatcherServlet contexts are children. The child inherit all beans from parent context. You can have more than one children and optionally one root context.

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.

The attribute name to store context

Both of them are stored as attributes in ServletContext, with different naming rule.

For root context, the attribute name is


The DispatcherServlet context has the name

org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)

See also