web.xml 通过contextConfigLocation配置spring 的方式

本文详细介绍了SSI框架(Struts2、Spring、iBatis)的配置文件路径设置及加载方式,包括不同框架配置文件的位置、数量及加载策略,并探讨了如何在web.xml中配置Spring的多个配置文件。

SSI框架配置文件路径问题:

struts2的 1个+N个  路径:src+src(可配置)      名称: struts.xml  + N
spring 的 1个           路径: src                          名称: applicationContext.xml
ibatis 的 1个+N个  路径: src+src(可配置)     名称: SqlMapConfig.xml + N

部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml,
运行时使用的是web-info/classes目录下的applicationContext.xml。

配置web.xml使这2个路径一致:

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

多个配置文件的加载
<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,
            classpath*:conf/spring/applicationContext_staff*.xml,
            classpath*:conf/spring/applicationContext_security.xml
            classpath*:conf/spring/applicationContext_modules*.xml
            classpath*:conf/spring/applicationContext_cti*.xml
            classpath*:conf/spring/applicationContext_apm*.xml
        </param-value>
    </context-param>

contextConfigLocation 参数定义了要装入的 Spring 配置文件。

 

首先与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。 
还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。这样程序看起来不会很乱。 
在web.xml中的配置如下: 
Xml代码 
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/applicationContext-*.xml</param-value>  
</context-param>

"**/"表示的是任意目录; 
"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。 
你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:

 <!-- Spring 的配置 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/spring/applicationContext-*.xml</param-value>
 </context-param>

 

web.xml中classpath:和classpath*:, 有什么区别?

 

classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

配置 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]。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值