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


Applicationcontext.xml - It is standard spring context file which contains all beans and the configuration  that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application.

 Spring-servlet.xml- It is a single entry point for Spring. DispatcherServlet scan this file and starts to load its component. It defines the bean and configuration that are only related to that servlet.

In Spring MVC application we chain them in below order −

          web.xml --> dispatcher servlet --> application context

Sr. No.KeyApplicationContext.xmlSpring-servlet.xml
1
Basic
applicationContext.xml  defines the beans that are shared among all the servlets. If your application has more than one servlet, then defining the common resources in the applicationContext.xml  would make more sense.
spring-servlet.xml  defines the beans that are related only to that servlet. Here it is the dispatcher servlet. So, your Spring MVC controllers must be defined in this file.
2
Scanning
We can add filter for package excluding controller package  
In spring-servlet.xml we include component scan for Controller package. 
3
References
Beans in ApplicationContext.xml can have reference beans in spring-servlet.xml
Beans in spring-servlet.xml  can reference beans in applicationContext.xml

Updated on: 09-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements