The relationship between WebApplicationContext, DispatcherServlet and the ServletContext of the web container

    To develop web applications with the spring framework, it is necessary to understand the relationship between WebApplicationContext, DispatcherServlet and the ServletContext of the web container.

I found a lot of posts, analyzed the code, and wrote a lot of relationships. In the end, it was still confusing.
I believe that the basic design ideas of a product are simple and clear, and under this guidance, complex codes are written.
The analysis code should finally return to the original idea of ​​the author, so as to know what is and why.


The specific code is not posted, and the summary is as follows:

1. The web container can be said to be the Servlet container: ServletContext, which must be present when starting tomcat. There are specific servlets configured in web.xml, which will be placed in the servlet container.

2. The dispatcherServlet is just a specific Servlet and must be installed in the Servlet container. Of course, the servlet container can install any other servlet, not necessarily the dispatcherServlet.

3. WebApplicationContext is an IOC container, which contains various bean components generated by spring's dependency injection. It can be said that there is no direct connection with the above Servlet container and specific Servlet. The usual IOC container is not only for web applications.

4. However, the specific servlet processing business in the web application generally uses the components in the IOC container, so there must be a direct reference relationship between the two containers. However, the specific content in the two containers should not have a direct reference relationship. Just like when you coordinate across departments, you always look for their leaders, not specific personnel directly.

5. Therefore, the WebApplicationContext container will be placed in the ServletContext container. This process is the listener in web.xml. During the startup process of the monitoring Servlet container, the IOC container is first generated and placed in the ServletContext.

6. If you are doing other java applications, you can use other IOC containers, such as ApplicationContext (ClasspathXmlApplicationContext, FileSystemXmlApplicationContext), you can initialize this IOC container object when you start your own application, and put it in a global place for reference. Other codes can take the components they want from the IOC.

Extension:
    When I look at the red-black tree, I can't see it all the time. There are many rules and many conversions. But when I looked at 2-3 trees, I understood red-black trees better.

    Later, I thought further, how did Rudolf Bell think about the invention of the red-black tree in 1972? To balance the binary tree, it is the process of civilians choosing leaders, and it is a gradual balance process from bottom to top, but it is not eager to balance and changes too much. Similar to the partial 2-3 tree, a node is added to slow the balance and improve performance. It is not the process of setting the leader first, and then arranging the seats from the top down. The balance of society may be the same.

Guess you like

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