浅谈Spring与web.xml

来源:互联网 发布:数据治理整体解决方案 编辑:程序博客网 时间:2024/05/21 07:24

在学习Spring的过程中,一个问题一直困扰着我,为啥在web.xml中写几行代码 就号称 “ 接入Spring ” 了。这几行代码又干了写什么。。下面先上一段配置,来分析分析

<?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 主容器 -->    <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>

web.xml中主要分为两个部分一个是主容器的创建(触发点为ContextLoaderListener),一个是servlet容器的创建(触发点为DispatcherServlet)。

在ContextLoaderListener中有个contextInitialized初始化容器的方法

一开始this.context为null,接着创建ApplicationContext

调用ApplicationContext的refresh()方法

DispatcherServlet则会在父类FrameworkServlet的initServletBean中调用initWebApplicationContext方法,剩下的流程和ContextLoaderListener流程一样

这里又是那个方法,,

这样就会进入容器部分。。

最后上两张图,先后创建的ApplicationContext的serializationId是不一样的,servlet的命名后面会跟上servlet-name

End...

0 0
原创粉丝点击