JakartaJEEWebDevelopment

Jakarta JEE8 Enterprise Web Development

View on GitHub

In a Spring MVC application, there are typically two contexts: the root application context and the web application context.

  1. Root Application Context: This context is loaded by the ContextLoaderListener when the application starts up. It contains beans that are shared among all servlets in the application. Typically, beans related to business logic, services, repositories, data access, and other non-web components are defined in the root application context. These beans are initialized when the application starts up and remain in memory throughout the application’s lifecycle.

  2. Web Application Context: This context is specific to the DispatcherServlet and is loaded when the DispatcherServlet is initialized. It contains beans that are scoped to the web layer, such as controllers, view resolvers, handler mappings, and other web-related components. Each DispatcherServlet has its own web application context. Beans defined in the web application context are typically specific to handling web requests and responses.

So, why would you use the root application context instead of a web application context?

Spring root application context helps to promote better organization, reusability, and maintainability of your application code.