There are two ways to start a spring container:
1, provide applicationcontext yourself to create the spring container
2. Configuring the Listener Startup in Web. xml
Org.springframework.web.context.ContextLoaderListener
First introduction (self-created)
Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Spring.xml") User user = (user) Context.getbean ()}
When the container is initialized by Classpathapplicationcontext, It loads the Spring.xml configuration according to the location, then parses the configuration file, generates the bean, registers the bean, and finally we get the object through Getbean, which is the same as the initialization process of the IOC container, resource positioning, resource loading, resource parsing, generating beans, Bean Registration
 
Let's talk about the second way of initializing:
The second is configured in the Web. xml file, starting with the start
<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class ></listener>
contextloaderlistener implements the Servletcontextlistener interface and implements two methods
//Initialize Webapplicationcontext container public void contextinitialized (servletcontextevent event) { this.initwebapplicationcontext (Event.getservletcontext ());} Destroy Webapplicationcontext container public void contextdestroyed (servletcontextevent event) { this.closewebapplicationcontext (Event.getservletcontext ()); Contextcleanuplistener.cleanupattributes (Event.getservletcontext ());}
Parameter servletcontextevent can directly get the context of ServletContext, which is the Java Web container
The Intitwebapplicationcontext method is called in the parent class, passed in the ServletContext, and the ServletContext is judged in the parent class method. to detect if there is a spring root context attribute in the ServletContext attribute, If there is an error, because it indicates that a root context has been started and the start conflict, so avoid repeating the boot!
(Servletcontext.getattribute (webapplicationcontext.root_web_application_context_attribute)! =) {illegalstateexception ()}
Spring Container startup process