- Are
applicationContext.xmlandspring-servlet.xmlrelated anyhow in Spring Framework? - Will the properties files declared in
applicationContext.xmlbe available toDispatcherServlet? - On a related note, why do I need a
*-servlet.xmlat all? Why isapplicationContext.xmlalone insufficient?
Are applicationContext.xml and spring-servlet.xml related anyhow in Spring Framework? Will the properties files declared
Share
Spring lets you define multiple contexts in a parent-child hierarchy.
The
applicationContext.xmldefines the beans for the “root webapp context”, i.e. the context associated with the webapp.The
spring-servlet.xml(or whatever else you call it) defines the beans for one servlet’s app context. There can be many of these in a webapp, one per Spring servlet (e.g.spring1-servlet.xmlfor servletspring1,spring2-servlet.xmlfor servletspring2).Beans in
spring-servlet.xmlcan reference beans inapplicationContext.xml, but not vice versa.All Spring MVC controllers must go in the
spring-servlet.xmlcontext.In most simple cases, the
applicationContext.xmlcontext is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there’s not really much point, unless you have a specific use for it.