applicationContext.xml、dispathcer-servlet.xml加载

一、applicationContext.xml和dispatcher-servlet.xml

applicationContext.xml是随ContextLoaderListener的加载而执行的,web

<!-- Spring -->
<!-- 配置Spring配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>
<!-- 配置Spring上下文监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring -->

而xxx-servlet.xml是随DispatcherServlet的加载而执行的,spring

<!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!-- 拦截全部/rest/* 的请求,交给DispatcherServlet处理,性能最好 -->
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

在web.xml中,加载顺序是listener>filter>servlet,因此applicationContext.xml先加载!spring-mvc

二、分红spring.xml(applicationContext.xml)、spring-mvc.xml(dispatcher-servlet.xml)、spring-mybatis.xml

首先ContextLoaderListener加载核心的spring.xml,以及数据源和事务管理等配置文件spring-batis.xmlmybatis

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
</context-param>


<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

而后DispatcherServlet加载spring-mvc.xmlmvc

<servlet>
    <description>spring-mvc</description>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-mvc.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
相关文章
相关标签/搜索