3

I am trying to understand the plumbing between a Spring framework container and a Servlet. I am just getting started and believe that a Spring MVC application can work with servlets, portlets etc.,

When a spring application using servlets is launched, I believe the application's world begins with the creation of a spring container, whose main function (not main exactly, but something similar) creates the beans, stitches them up etc., It also then creates a servlet, the DispatcherServlet, which starts accepting connections and routes requests to other controllers in the spring application. Since the spring container does not actively manage the servlet, the DispatchServlet cannot be considered to be part of the spring container. Is the above understanding of mine correct ?

It would be great if anyone can shine more light on the plumbing between Spring framework and (say) and embedded servlet container such as Tomcat/getty. Thank You !

Venkat
  • 31
  • 2
  • Check http://stackoverflow.com/questions/21714290/how-is-spring-actually-bootstrap . Running embedded servlet container does not change a thing. – Pavel Horal Aug 01 '14 at 10:25
  • Here's another helpful description: http://stackoverflow.com/questions/19826228/spring-web-security-web-xml-mvc-dispatcher-bean-is-created-twice – Sotirios Delimanolis Aug 02 '14 at 04:17

1 Answers1

1

Spring itself isn't a container, its a framework. It does have an IoC container however and this container is built on beans and contexts. For instance in a web application, there are two contexts :

  • the DispatcherServlet context
  • the root application context

So to anwser your question, the dispatcher servlet is just a standard servlet but it has its own spring IoC context which is where controllers, view templates etc are declared and wired. if you look at the web.xml of a spring web application you see something like this...

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

This is just a standard servlet declaration and you provide it the xml file it is to use to create its own IoC context.

That clear things up?

dectarin
  • 986
  • 5
  • 15