Spring Container startup process

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.