Question

Am trying to understand the control flow between web.xml and applicationcontext.xml of a web application. Can someone please explain which loads first?

Was it helpful?

Solution

Spring MVC is a framework built on top of the Servlet API. As such, it requires a Servlet container.

Your Servlet container finds the web.xml or uses any other deployment strategy (depends on container), finds the Servlet implementations to instantiate (or through @WebServlet) and instantiates them.

One of these Servlet implementations is Spring's DispatcherServlet which, as part of its initialization, generates an ApplicationContext which it then uses to configure its controllers and dispatch requests.

OTHER TIPS

The servlet container (Tomcat, Jetty, etc.) will read the web.xml at startup and instantiate the various servlet / filters / listeners defined there. One of these filters or servlet is most likely going to be provided by Spring which will then read the applicationContext.xml and initialize the Spring context.

For example, your web.xml may contain something like:

<servlet>
   <servlet-name>dispatcher</servlet-name>
   <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
   </servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
 </servlet>

That servlet will be reading and creating the Spring context inside which the Spring bean "live".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top