Talking about Spring and web.xml

In the process of learning Spring, a question has been bothering me, why is it called "accessing Spring" by writing a few lines of code in web.xml. What are these lines of code doing? . Let's first configure the previous paragraph to analyze and analyze

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>xxx</display-name>
    <!-- part.1 main container-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:application-pre.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- part.2 servlet容器 -->
    <servlet>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>springmvc</description>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


</web-app>

The web.xml is mainly divided into two parts: one is the creation of the main container (the trigger point is ContextLoaderListener), and the other is the creation of the servlet container (the trigger point is DispatcherServlet).

There is a contextInitialized method for initializing the container in ContextLoaderListener

At first this.context is null, then create ApplicationContext

Call the refresh() method of ApplicationContext

DispatcherServlet will call the initWebApplicationContext method in the initServletBean of the parent class FrameworkServlet, and the rest of the process is the same as the ContextLoaderListener process

Here is that method again,

This will enter the container section. .

In the last two pictures, the serializationId of the ApplicationContext created successively is different, and the name of the servlet will follow the servlet-name

End...

Guess you like

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