The relationship between the spring context and the spring MVC context and the Web application context ServletContext

Source: Internet
Author: User

To get a good understanding of the three context relationships, you need to be familiar with how spring starts up in the Web container. Spring is actually the launch process of its IOC container, and for web programs, the IOC container initiation process is the process of establishing the context.

Spring Start-up process:
  1. First, for a Web application that is deployed in a Web container, the Web container provides its global context, which is ServletContext, which provides the hosting environment for the following spring IOC container;

  2. Second, a contextloaderlistener is provided in Web. Xml. When the Web container starts, the container initialization event is triggered, and Contextloaderlistener hears the event and its Contextinitialized method is called, in which spring Initializes a startup context, This context is referred to as the root context, Webapplicationcontext, which is an interface class, specifically, its actual implementation class is Xmlwebapplicationcontext. This is the spring IOC container, whose corresponding bean-defined configuration is specified by the Context-param tag in Web. Xml. After the IOC container is initialized, Spring takes the Webapplicationcontext.rootWEBapplicationCONTEXTattribute as the property key, Store it in ServletContext for easy access;

  3. Again, after the Contextloaderlistener listener is initialized, Start initializing the servlet configured in Web. XML, this servlet can be configured with multiple, for example, the most common Dispatcherservlet, the servlet is actually a standard front-end controller to forward, match, and process each servlet request. The Dispatcherservlet context, when initialized, establishes its own IOC context for holding spring MVC-related beans. The Webapplicationcontext.rootWEBApplication context is used when establishing Dispatcherservlet's own IOC contexts Attribute first gets the previous root context (that is, Webapplicationcontext) from ServletContext as the parent context of its own context. Once you have this parent context, initialize the context that you hold. The work of this dispatcherservlet initializing its own context can be seen in its Initstrategies method, which is about initializing processor mappings, view parsing, and so on. The default implementation class for the context that the servlet holds itself is also mlwebapplicationcontext. After initialization, spring is associated with the name of the servlet (not simply a servlet named key, but through some transformations, the specific self-view source) property is the property key, and it is stored in ServletContext for later use. Each servlet thus holds its own context, which has its own separate bean space, while each servlet shares the same bean, which is defined by the root context (the context initialized in step 2nd).

After the initialization of the spring context, the relationship between the three contexts should be understood. If still not very clear, I can not help, only to see the code on their own.

This article turns from https://segmentfault.com/q/1010000000210417 thanks to the author, very classic

ServletContext is a great environment for Web applications that store objects at the level of the entire Web application and do not know if that is true.

Applicationcontext,webapplicationcontext is spring's beanfactory, from the name can know the difference pull, one is to support the web features of the beanfactory.

Spring acquires several methods of Webapplicationcontext and ApplicationContext:

Method One: Save the ApplicationContext object at initialization time
Code:
ApplicationContext ac = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");
Ac.getbean ("Beanid");
Description: This approach applies to standalone applications that use the spring framework, requiring the program to manually initialize the spring with a configuration file.

Method Two: Get the ApplicationContext object from the tool class provided by spring
Code:
Import Org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext AC1 = webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext SC);
ApplicationContext AC2 = webapplicationcontextutils.getwebapplicationcontext (ServletContext SC);
Ac1.getbean ("Beanid");
Ac2.getbean ("Beanid");
Description
This method is suitable for the B/s system with spring Framework, obtains the ApplicationContext object through the ServletContext object, and obtains the required class instance through it.

The relationship between the spring context and the spring MVC context and the Web application context ServletContext

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.