Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the intended use of `servlet-context.xml`, `root-context.xml` and `web.xml`?

I a new to Java Spring MVC web development. I am kind of confused by the 3 config files below. They are auto created by the STS webmvc project template.

  • What's the intended use of them?
  • Why do we need 3 config files rather than a single one?
  • Is there any special reason for their different locations?

enter image description here

like image 930
smwikipedia Avatar asked Jan 12 '15 00:01

smwikipedia


1 Answers

root-context.xml is the Spring Root Application Context Configuration. It's optional. It's for configuring your non-web beans. You need it for Spring Security or OpenEntityManagerInView Filter though. It would be better to place it in meta-inf/spring.

servlet-context.xml is the Spring Web Application Context Configuration. It's for configuring your Spring beans in a web application. If you use root-context.xml, you should put your non-web beans in root-context.xml, and web beans in servlet-context.xml.

web.xml is for configuring your servlet container, such as Tomcat. You need this one too. It's for configuring servlet filters and the servlet. web.xml is loaded first, then optionally loads your root context, then loads your web context.

You can avoid using xml by using JavaConfig.

like image 92
Neil McGuigan Avatar answered Oct 24 '22 08:10

Neil McGuigan