1.用途不同

applicationContext.xml文件通常用于加载spring系统级别的组件,比如bean的初始化。 spring-mvc.xml文件通常用于加载controller层需要的类,比如拦截器,mvc标签加载的类。

2.加载位置不同

applicationContext.xml加载在标签中,作为FrameworkServlet的参数属性。 spring-mvc.xml文件当做DispatcherServlet的参数属性进行加载。

applicationContext.xml是全局的,应用于多个servelet,配合listener一起使用。 web.xml中配置如下:

<!-- 配置监听器 -->
<listener>        
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

spring-mvc.xml是SpringMVC的配置,web.xml中配置如下:

<!--配置SpringMVC DispatcherServlet-->
<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>

application-context.xml这个一般是采用非SpringMVC架构,用来加载Application Context。 如果直接采用SpringMVC,只需要把所有相关配置放到spring-mvc.xml中就好,一般SpringMVC项目用不到多个serverlet。

下面是对web.xml配置文件的说明:

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

ContextLoaderListener是Spring的监听器,它的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

<context-param>
     <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
     <!--<param-value>classpath*:config/applicationContext.xml</param-value> -->       
</context-param>

这段配置是用于指定applicationContext.xml配置文件的位置,可通过context-param加以指定。

如果applicationContext.xml配置文件存放在src目录下,那么在web.xml中的配置就如下所示:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

如果applicationContext.xml配置文件存放在WEB-INF下面,那么在web.xml中的配置就如下所示:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>

需要注意的是,部署到应用服务器后,src目录下的配置文件会和class文件一样,自动copy到应用的classes目录下,spring的配置文件在启动时,加载的是web-info目录下的applicationContext.xml运行时使用的是web-info/classes目录下的applicationContext.xml。因此,不管applicationContext.xml配置文件存放在src目录下,还是存放在WEB-INF下面,都可以用下面这种方式来配置路径:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>

注意: 1.classpath是指 WEB-INF文件夹下的classes目录 2.classpath与classpath的区别: classpath:指classes路径下文件 classpath:除了classes路径下文件,还包含jar包中文件


当有多个配置文件加载时,可采用下面代码来配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> 
         classpath*:conf/spring/applicationContext_core*.xml, 
         classpath*:conf/spring/applicationContext_dict*.xml,
         classpath*:conf/spring/applicationContext_hibernate.xml,
         ......
    </param-value>
 </context-param>

也可以用下面的这种方式:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:**/applicationContext-*.xml</param-value>
</context-param>

注: "/"表示的是任意目录; "/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。 Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。