Victor
Victor

Reputation: 17107

How does the web container manage the lifecycle of a spring controller

If i write a simple servlet application, in my servlet class i extend the http servlet. This lets the container know that my class is a servlet and it will manage the 'lifecycle' of the servlet. init, doget(), destroy() etc.

But the Spring MVC framweork controller class does not extend any servlet class. it is jsut a POJO with it s own custom methods. Now I can call those methods individually using Requestmapping.

But will this spring controller class be 'mananged' by the container in a same way a servlet lifecyle is managed?

Upvotes: 3

Views: 1371

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280132

But will this spring controller class be 'mananged' by the container in a same way a servlet lifecyle is managed?

Not directly. Then entry point of a Spring MVC application is typically a DispatcherServlet*. This class extends (not directly, but through inheritance) HttpServlet. You declare it as you would any other Servlet, typically in web.xml.

However you don't declare it by itself. You provide a Spring ApplicationContext from which the DispatcherServlet can go and get the @Controller annotated classes it will use to handle requests.

The DispatcherServlet handler stack is pretty big. There are many components involved. The official Spring MVC is an excellent document. You should read it.

*I say typically because Spring provides other handlers, HttpRequestHandler for example.


Additional Reading:

  1. Spring MVC and Servlets 3.0 - Do you still need web.xml?
  2. What happens behind the scenes of deploying a Spring MVC application to tomcat outside of eclipse?
  3. What's the difference between @Component, @Repository & @Service annotations in Spring?
  4. Spring MVC: difference between <context:component-scan> and <annotation-driven /> tags?
  5. Difference between <context:annotation-config> vs <context:component-scan>
  6. ContextLoadListener and DispatcherServlet

Upvotes: 3

Related Questions