6

Why do we need applicationContext.xml in Spring?

In what situation would we use it? Do you have an example?

What is the difference between applicationContext.xml and spring-servlet.xml?

How can we compare applicationContext.xml in Spring with Struts.xml in Struts for easy understanding?

asteri
  • 11,402
  • 13
  • 60
  • 84
  • 1
    What research on this have you done yourself? What don't you understand, having done your research, and why don't you understand it? – asteri Aug 07 '13 at 11:40
  • http://viralpatel.net/blogs/tutorial-spring-3-mvc-introduction-spring-mvc-framework/ looks good as first overwiev – ice Aug 07 '13 at 11:40

3 Answers3

8

Why do we need applicationContext.xml in Spring?

In the early days of Spring framework, Application context i.e the various weave and settings necessary to bootstrap, coordinate and control all objects, where done using XML file. Although one can break various settings and dependency injection into several context files, this process has been made easier In Spring 2.5 and later by annotation-driven settings.

What is the difference between applicationContext.xml and spring-servlet.xml?

In a MVC based project, again if you're not using annotation-driven weaving mechanism for your project, all your endpoint servlets can be setup in the spring-servlet.xml. Note that the name of the file is always self chosen.

How can we compare applicationContext.xml in Spring with Struts.xml in Struts for easy understanding?

They are both similar in terms of what they're trying to achieve. i.e a central location for the application bootstrap settings. Similarly, all settings can be tiered into different files to make it modular.

Bitmap
  • 12,402
  • 16
  • 64
  • 91
4

applicationContext comes from Spring Framework: it manages the business/DAO beans.

spring-servlet comes from Spring MVC: it manages the web beans.

sp00m
  • 47,968
  • 31
  • 142
  • 252
3

A Web application can have many servlets running at the same time, therefore:

spring-servlet.xml will hold beans only visible to a particular servlet.

You could have many different servlets running

spring-servlet2.xml
spring-servlet3.xml
messaging-servlet.xml 

etc.

applicationContext.xml will hold application wide beans. Therefore all the servlets running will have access to the beans defined in applicationContext.xml. However, this is a one way dependency, your servlets can access you applicationContext.xml beans but your applicationContext cannot access any of your servlet beans.

jax
  • 37,735
  • 57
  • 182
  • 278