Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the Spring Framework, understanding the roles and differences between applicationContext.xml and spring-servlet.xml is crucial for structuring Spring applications effectively. While both files are used for configuration in a Spring based application, they serve distinct purposes, and are used in different contexts.
Understanding applicationContext.xml
applicationContext.xml serves as the root context configuration file in a Spring application. This file generally contains the bean definitions that are meant to be shared across the entire application. It configures the backbone of your application and might include service layer implementations, data access objects (DAOs), data sources, infrastructure beans (such as a JdbcTemplate, EntityManagerFactory, etc.), and more.
Essentially, applicationContext.xml is loaded by a ContextLoaderListener defined in the web.xml which is a standard configuration file used in Java-based web applications. This listener is responsible for bootstrapping the root context before any servlet-specific contexts, such as those defined in spring-servlet.xml, are initialized.
Understanding spring-servlet.xml
spring-servlet.xml, on the other hand, is typically the configuration file specifically for a Spring DispatcherServlet. This file often contains web-related beans such as controllers, view resolvers, handler mappings, interceptors, and more. The DispatcherServlet serves as the front controller of a Spring-based web application. It dispatches incoming HTTP requests to the appropriate handlers and views.
The spring-servlet.xml is loaded each time the DispatcherServlet is initialized. If your application has multiple DispatcherServlets, each can have its own dedicated servlet configuration file, normally named after the servlet itself (e.g., myapp-servlet.xml for a servlet named myapp).
Key Differences
Here is a simple table comparing some key points regarding applicationContext.xml and spring-servlet.xml:
| Feature | applicationContext.xml | spring-servlet.xml |
| Scope | Application-wide (root context) | Servlet-specific (web context) |
| Managed Components | Services, Repositories, Resources, Integration Components | Web components like Controllers, View Resolvers, Handlers |
| Loaded By | ContextLoaderListener in web.xml | DispatcherServlet |
| Influence | Shared across all the servlets and filters | Specific to one servlet, does not interfere with others |
Example Usage
Consider a simple web application with database functionalities and a few web pages. Your applicationContext.xml might look something like this:
And your spring-servlet.xml would be configured like:
From the above configurations, it's clear that applicationContext.xml is managing core components such as services and data sources which are required across the entire application, whereas the spring-servlet.xml is managing web components which are specific to the servlet.
Conclusion
Understanding the distinction and relationship between applicationContext.xml and spring-servlet.xml is vital for properly structuring Spring Framework applications. By appropriately segmenting application concerns across these two contexts, developers can ensure more maintainable, scalable, and clean application architectures.
Related reading
- Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll
- Difference between Codahale metrics and Dropwizard metrics
- Difference between CompletableFuture, Future and RxJava''s Observable
- Difference between CompletableFuture, Future and RxJava''s Observable
- Difference between CompletableFuture, Future and RxJava''s Observable
- Difference between <contextannotation-config> and <contextcomponent-scan>
- Difference between Controller and RouterFunction in Spring 5 WebFlux
- Difference between DeferedResult and CompletableFeature

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.