Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will we use applicationContext.xml in Spring? [duplicate]

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?

like image 644
Praveen P Moolekandathil Avatar asked Aug 07 '13 11:08

Praveen P Moolekandathil


3 Answers

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.

like image 68
Bitmap Avatar answered Oct 19 '22 07:10

Bitmap


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

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

like image 4
sp00m Avatar answered Oct 19 '22 06:10

sp00m


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.

like image 3
jax Avatar answered Oct 19 '22 06:10

jax