spring-mvc.xml与applicationContext.xml的区别

本文详细介绍了Spring框架中applicationContext.xml和spring-mvc.xml两个配置文件的作用和区别。applicationContext.xml主要用于加载系统级组件,而spring-mvc.xml则专注于配置Controller层。两者的加载位置不同,前者由ContextLoaderListener监听器处理,后者在DispatcherServlet中加载。此外,文章还提供了web.xml中配置这两个文件的示例,并解释了如何指定配置文件的位置。对于多个配置文件的加载,文章也给出了相应的配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、用途不同

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

二、加载位置不同

1️⃣applicationContext.xml加载在标签中,作为FrameworkServlet的参数属性。其是全局的,应用于多个servlet,配合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>

2️⃣spring-mvc.xml当做DispatcherServlet的参数属性进行加载。其是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>

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

三、对 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配置文件都放在一个统一的目录下,也可以分模块创建。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JFS_Study

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值