web.xml中的contextConfigLocation在spring中的作用

本文介绍如何在Spring项目中配置多个XML文件,并解释如何在这些文件间进行引用。通过web.xml中的ContextLoaderListener,可以指定多个配置文件路径,利用逗号或空格分隔。此外,还介绍了不同配置文件间如何通过<ref>标签实现bean的引用。

在web.xml中通过contextConfigLocation配置spring,contextConfigLocation
参数定义了要装入的 Spring 配置文件。

如果想装入多个配置文件,可以在 <param-value>
标记中用逗号作分隔符。
在web.xml里配置Listener

xml 代码如下: 
  <listener>  
       <listener-class> org.springframework.web.context.ContextLoaderListener listener-class >  
  </listener>

如果在web.xml里给该Listener指定要加载的xml,如:

xml代码如下:

<!-- spring config -->

      <context-param>

           <param-name>contextConfigLocation</param-name> 

           <param-value>classpath:applicationContext.xml</param-value>

      </context-param>

则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。

但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。

 

在一个团队使用Spring的实际项目中,应该需要多个Spring的配置文件,如何使用和交叉引用的问题:

    多个配置文件可以在web.xml里用空格分隔写入,如:
    <CONTEXT-PARAM>
         <PARAM-NAME>contextConfigLocation</PARAM-NAME>
         <PARAM-VALUE>
               applicationContext-database.xml,applicationContext.xml
         </PARAM-VALUE>  
     </CONTEXT-PARAM>
     多个配置文件里的交叉引用可以用ref的external或bean解决
   例如:

 

applicationContext.xml
    <bean id="userService" class="domain.user.service.impl.UserServiceImpl"> 
        <property name="dbbean">
             <ref bean="dbBean"/>
         </property> 
    </bean>

dbBean在applicationContext-database.xml中

在配置 Spring 框架和 Spring MVC 时,`web.xml` 文件是 Web 应用程序的部署描述符,用于定义 Spring 的上下文加载方式以及 Spring MVC 的前端控制器 `DispatcherServlet`。以下是详细的配置方式。 ### 配置 Spring 上下文 Spring 的核心配置文件(例如 `applicationContext.xml`)通常包含服务层和数据访问层的 Bean 定义。这些配置文件需要通过 `<context-param>` 标签进行指定,确保 Spring 的上下文在应用启动时加载: ```xml <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:springcfg/applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 这段配置的作用是告诉 Spring 去类路径下的 `springcfg` 目录中查找名为 `applicationContext.xml` 的配置文件,并通过 `ContextLoaderListener` 监听器加载 Spring 的上下文[^1]。如果有多个配置文件,可以通过逗号或冒号分隔的方式指定多个文件路径[^3]。 ### 配置 Spring MVC Spring MVC 的配置主要通过 `DispatcherServlet` 来实现,它负责接收所有的 HTTP 请求并协调 Spring MVC 的各个组件。需要在 `web.xml` 中定义 `DispatcherServlet` 并指定其对应的配置文件(例如 `springMVC.xml`): ```xml <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springcfg/springMVC.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> ``` 上述代码中,`DispatcherServlet` 被命名为 `springmvc`,并通过 `<init-param>` 指定其配置文件路径为 `classpath:springcfg/springMVC.xml`。`<load-on-startup>` 标签确保 `DispatcherServlet` 在应用启动时加载[^1]。最后通过 `<servlet-mapping>` 将所有请求映射到 `springmvc` 处理。 ### 配置注意事项 1. **Spring 上下文与 Spring MVC 的关系**: Spring 的上下文通常用于加载服务层和数据访问层的 Bean,而 Spring MVC 的上下文则专注于加载 Web 层相关的 Bean(如控制器)。Spring MVC 的上下文会继承 Spring 的上下文,但不会覆盖它[^2]。 2. **配置文件路径**: 配置文件的路径可以通过 `classpath:` 指定类路径下的文件,也可以通过文件系统的绝对路径来指定。多个配置文件可以通过逗号、冒号等分隔符分隔[^3]。 3. **监听器的作用**: `ContextLoaderListener` 是 Spring 提供的监听器,负责初始化 Spring 的上下文。它会在应用启动时加载 `contextConfigLocation` 指定的配置文件[^1]。 ###
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值