Deep analysis of Spring and spring MVC containers

Source: Internet
Author: User

Description of the Web container in the spring Official document spring configuration file default name--applicationcontext.xml reference link: https://www.cnblogs.com/hujunzheng/p/5673377. Htmlspring 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 initialization of this IOC container, Spring takes webapplicationcontext.rootwebapplicationcontextattribute as the attribute key and stores it in the ServletContext for easy access;
Webapplicationcontextutils.getwebapplicationcontext (ServletContext)
    1. 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. When establishing Dispatcherservlet's own IOC context, Use Webapplicationcontext.rootwebapplicationcontextattribute to get the previous root context (i.e. Webapplicationcontext) from the ServletContext first The parent context as 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).
When the servlet executes the ApplicationContext Getbean, if the corresponding bean is not found in its own context, it is found in the parent applicationcontext. This also explains why we can get the beans in the dispatcherservlet that correspond to the ApplicationContext in the Contextloaderlistener. (Controller can use all of spring's beans) Ordinary Java classes are not combined with spring, and intelligently get spring-managed beans through ApplicationContext

Deep analysis of Spring and spring MVC containers

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.