Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Spring MVC need at least two contexts?

Tags:

In Spring MVC, there are two contexts. One is the application context or global context which is booted up by ContextLoaderListener. It takes all the configuration files mentioned in contextConfigLocation parameter.

Now if you are using Spring MVC as well, then Dispatcher servlet is required, which boots up another container which is also known as web application container. This container takes the global container as a parent.

When integrating struts1 with spring, there is only one context. Why does spring mvc need two? Is it possible to use only one context when using spring mvc?

thanks!

like image 443
liam xu Avatar asked Sep 08 '13 10:09

liam xu


People also ask

Why do we need context in Spring?

Spring contexts are also called Spring IoC containers, which are responsible for instantiating, configuring, and assembling beans by reading configuration metadata from XML, Java annotations, and/or Java code in the configuration files.

Can we have two application context in Spring?

We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.

What is Spring MVC context?

What is WebApplicationContext in Spring MVC? WebApplicationContext in Spring is a web-aware ApplicationContext i.e it has Servlet Context information. In a single web application, there can be multiple WebApplicationContext. That means each DispatcherServlet is associated with a single WebApplicationContext.

How many types of context are there in Spring?

As you are using Spring boot, there is only one context by default: ApplicationContext .


2 Answers

Having a root web application context plus a child servlet context is just an option. If you know that your application won't have a second servlet, it's arguably simpler to have one single Spring context for the whole web application.

You can achieve that setup by simply removing the ContextLoaderListener (and the accompanying contextConfigLocation context-param) from your web.xml and moving all bean definitions into the xml defining the servlet context ([servlet-name]-servlet.xml).

This is possible, because the FrameworkServlet (super-class of DispatcherServlet) doesn't care if there is a root application context when creating the servlet context. It just relays the root context as the parent if available. See related code here.

like image 191
zagyi Avatar answered Sep 23 '22 00:09

zagyi


Imagine you had two separate Dispatchers, each serving a different purpose, and each having its own dependencies. You would configure those independently using separate contexts.

If there is any shared configuration, this can go in the 'global' context.

I don't think it's possible to have only one context using DispatcherServlet, as it creates its own context and links it to the parent context (via the FrameworkServlet superclass).

FrameworkServlet.createWebApplicationContext

like image 31
Paul Grime Avatar answered Sep 23 '22 00:09

Paul Grime