What is the purpose of having multiple spring "application context"

1.3k Views Asked by At

Per the Spring Documentation, application context is:

Central interface to provide configuration for an application. This is read-only while the application is running, but maybe reloaded if the implementation supports this.

In some applications, there are multiple application contexts. What's the purpose and benefit of having multiple application contexts? I want to understand the logic behind it. Why would one do it?

P.S: In spring doc use is written. I want to know the pros of having multiple application contexts and the rationale behind it.

3

There are 3 best solutions below

0
On BEST ANSWER

The root context is the parent of every dispatcher servlet context/child context. Beans defined in root context are visible to each dispatcher servlet context/child context but not vice-versa.

Typically, root context is used to define all cross-cutting beans such as security, transactions, and other configurational beans, while the dispatcher context or child context contains those beans that are specifically related to MVC.

We create multiple dispatcher servlets when we need multiple sets of MVC configuration. For e.g. we may have a REST API alongside a traditional MVC application or an unsecured and a secure section of a website.

enter image description here

0
On

Cannot comment so putting the answer:-

Java Spring multiple ApplicationContext

Hope this answer your question

0
On

It is useful to implement a layered architecture (model objects, data access, services, web services, mvc app etc).

As M.Deinum said it is good to have one root context that loads the others and helps keeping them separated.

Here is the official doc for the architecture of Spring Framework applications: https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/overview.html