Spring Framework
applicationContext.xml
spring-servlet.xml
Java
Programming Concepts

Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

FeatureapplicationContext.xmlspring-servlet.xml
ScopeApplication-wide (root context)Servlet-specific (web context)
Managed ComponentsServices, Repositories, Resources, Integration ComponentsWeb components like Controllers, View Resolvers, Handlers
Loaded ByContextLoaderListener in web.xmlDispatcherServlet
InfluenceShared across all the servlets and filtersSpecific 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:

xml
1<beans>
2    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
3        <!-- configuration properties -->
4    </bean>
5    <bean id="myService" class="com.example.MyService">
6        <property name="dataSource" ref="dataSource"/>
7    </bean>
8</beans>

And your spring-servlet.xml would be configured like:

xml
1<beans>
2    <bean id="myController" class="com.example.MyController">
3        <property name="service" ref="myService"/>
4    </bean>
5    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
6        <property name="prefix" value="/WEB-INF/views/"/>
7        <property name="suffix" value=".jsp"/>
8    </bean>
9</beans>

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.


Course illustration
Course illustration

All Rights Reserved.