Smrita
Smrita

Reputation: 1279

Could someone please describe Spring web app's lifecycle in detail to me?

I have been learning Spring for about a week now and I don't seem to understand a spring web app's life cycle properly.Could someone explain it to me

I have been go-ogling this but have not been able to understand it completely.Some of the websites say that once web container loads a web app then context loader initializes Spring framework.

Anyways the point is i haven't understood this properly.I am assuming that beans gets created when spring is instantiated(by default, and not in case of lazy loading) but how ?

Upvotes: 1

Views: 843

Answers (1)

JB Nizet
JB Nizet

Reputation: 692023

Spring webapps are like non-Spring webapps. They have a standard webapp configuration (either using web.xml, or using more recent Java configuration).

This configuration defines a servlet, the Spring Dispatcher servlet, and maps it to a set of URL patterns. When the webapp is deployed, this servlet is thus initialized by the container, and the standard webapp listeners are called. Spring then loads its own configuration, which is a Spring context configuration. This thus starts a Spring context, instantiates a whole lot of Spring beans, and wires them together.

Some of the Spring beans instantiated and wired are Spring controllers, mapped to a specific URL or set of URLs (and HTTP methods, accept headers, etc.). So when a request comes into Tomcat, Tomcat first locates the web application that should handle it. Then it identifies which servlet in the application should handle it. If the DispatcherServlet is the handler, it analyzes the request and once again dispatches it to the appropriate Spring controller.

Upvotes: 3

Related Questions