In-depth analysis of Spring and the Spring MVC container

Description of the web container in the official spring documentation
The default name of the spring configuration file - applicationContext.xml
Reference link:
Spring's startup process:
  1. First of all, for a web application, it is deployed in the web container, and the web container provides a global context, which is the ServletContext, which provides the host environment for the subsequent spring IoC container;
  2. Secondly, contextLoaderListener will be provided in web.xml. When the web container starts, the container initialization event will be triggered. At this time, the contextLoaderListener will listen to this event, and its contextInitialized method will be called. In this method, spring will initialize a startup context, which is called the root context, namely WebApplicationContext , which is an interface class, to be precise, its actual implementation class is XmlWebApplicationContext. This is spring's IoC container, and the configuration of its corresponding bean definition is specified by the context-param tag in web.xml. After the IoC container is initialized, spring uses WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE as the attribute Key and stores it in the ServletContext for easy access; 
WebApplicationContextUtils.getWebApplicationContext(servletcontext)
  1. Once again, after the contextLoaderListener listener is initialized, it starts to initialize the Servlet configured in web.xml. This servlet can be configured with multiple ones. Take the most common DispatcherServlet as an example, this servlet is actually a standard front-end controller for forwarding, Match and process each servlet request. The DispatcherServlet context will establish its own IoC context during initialization to hold spring mvc-related beans. When establishing DispatcherServlet's own IoC context, it will use WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE to obtain the previous root context (ie WebApplicationContext) from ServletContext as the parent context of its own context. After you have this parent context, initialize the context you hold. The work of this DispatcherServlet to initialize its own context can be seen in its initStrategies method. The general work is to initialize processor mapping, view resolution, and so on. The default implementation class of the context held by this servlet is also mlWebApplicationContext. After initialization, spring uses the attribute related to the name of the servlet (here, it is not simply named as the servlet key, but through some conversion, you can view the source code by yourself) as the attribute key, and also save it in the ServletContext, so that subsequent use. In this way, each servlet holds its own context, that is, has its own independent bean space, and each servlet shares the same beans, that is, those beans defined by the root context (the context initialized in step 2).
When the servlet executes the getBean of the ApplicationContext, if it cannot find the corresponding bean in its own context, it will look for it in the parent ApplicationContext. This also explains why we can get the beans in the ApplicationContext corresponding to the ContextLoaderListener in the DispatcherServlet. (controller can use all spring beans)
Ordinary java classes are not combined with spring, and intelligently obtain spring-managed beans through ApplicationContext
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324791450&siteId=291194637