WEB项目web.xml文件中classpath: 跟classpath*:使用的区别

本文详细解释了在Spring框架中classpath与classpath*的区别,并通过具体的示例说明了这两种配置方式如何影响XML配置文件的加载过程。

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

引用一篇很不错的文章:http://blog.csdn.net/wxwzy738/article/details/16983935

首先 classpath是指 WEB-INF文件夹下的classes目录

classpath 和 classpath* 区别

classpath:只会到你指定的class路径中查找文件;

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

 

举个简单的例子,在我的web.xml中是这么定义的:classpath*:META-INF/spring/application-context.xml

那么在META-INF/spring这个文件夹底下的所有application-context.xml都会被加载到上下文中,这些包括META-INF/spring文件夹底下的 application-context.xml,META-INF/spring的子文件夹的application-context.xml以及jar中的application-context.xml。

 

如果我在web.xml中定义的是:classpath:META-INF/spring/application-context.xml

那么只有META-INF/spring底下的application-context.xml会被加载到上下文中。

 

项目实践中用的比较多就是在web.xml文件中配置spring监听器,让它读取*.xml文件,加载其中的bean.

举例如下

首先:在ssh框架搭建好之后,在web.xml文件中进行如下配置

<!--系统自动加载文件 -->

这里使用的是classpath*:的形式

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

 <!--配置spring的context监听器  -->

  <listener>

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

</listener-class>

  </listener>

在项目的.classpath文件中有如下的配置

补充:

1.)关于.classpath文件的查看可以去项目存放的位置(workplace)下查看,也可以在eclipse中navigator视图中进行查看

2.)navigator视图是在window---->show view----->other------>navigator中可以找到.

<?xml version="1.0" encoding="UTF-8"?>
<classpath>

<!--系统默认的class文件加载路径,不能够删除-->
<classpathentry kind="src" path="src"/>

<!--自己新添加的class文件加载路径-->
<!--<classpathentry kind="src" path="config"/>-->

这里可以添加classpath的路径,例如我的配置文件路径时放在了与src同级的目录下,你也可以指定自己的文件存放路径.

项目启动时候,系统会依据.classpath文件中指定的src的路径依次查找所要加载的配置文件。并加载其中的bean.

 

转载于:https://my.oschina.net/dccjll/blog/1543661

<think>我们根据用户需求调整web.xml文件,重点在于:1.保留原有架构和路径参数2.确保contextConfigLocation包含指定的三个配置文件:'spring-common-config.xml','spring-security-config.xml','quartz-config.xml'3.其他配置如欢迎页面、过滤器、Servlet、错误页面、安全约束等保持原样或适当补充根据引用[2]和[4],我们可以将多个配置文件用逗号分隔,放在context-param的param-value中。注意:用户提供的引用中,contextConfigLocation的配置方式有两种,一种是使用classpath*:加上路径,另一种是直接classpath:。这里我们按照用户上一次的示例,使用classpath:,并且不加通配符(因为用户明确指定了文件名)。因此,我们将context-param部分调整为:<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-common-config.xml,classpath:spring-security-config.xml,classpath:quartz-config.xml</param-value></context-param>同时,保留其他配置(如监听器、过滤器、Servlet等)并保持原样。另外,用户引用的内容中提到有多个配置文件,但用户指定了这三个,所以我们就用这三个。下面给出完整的web.xml,其中contextConfigLocation按上述要求修改,其他部分参考用户上一次的示例,并保持结构完整。</think> ### 优化后的完整 `web.xml` 配置文件 根据需求保留原有路径参数,同时确保 `contextConfigLocation` 包含指定文件,优化后配置如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 应用显示名称 --> <display-name>Enterprise Application</display-name> <!-- 全局上下文参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-common-config.xml, classpath:spring-security-config.xml, classpath:quartz-config.xml </param-value> </context-param> <!-- Spring 上下文监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 欢迎文件 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 字符编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC 前端控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 会话配置 --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- 错误页面 --> <error-page> <error-code>404</error-code> <location>/error/404</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500</location> </error-page> </web-app> ``` ### 关键优化点说明 1. **核心配置保留** 确保 `contextConfigLocation` 包含三个指定配置文件[^2][^4]: ```xml <param-value> classpath:spring-common-config.xml, classpath:spring-security-config.xml, classpath:quartz-config.xml </param-value> ``` 2. **配置文件加载顺序** 通过 `ContextLoaderListener` 按顺序加载指定配置文件,保持原有配置路径不变[^4]: ```xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 3. **文件格式支持** 使用逗号分隔符支持多配置文件加载,适应模块化配置需求[^2]: ```xml classpath:spring-common-config.xml, classpath:spring-security-config.xml, classpath:quartz-config.xml ``` 4. **架构兼容性** 保持与原有分层配置方案的兼容性: - Spring MVC 配置:`/WEB-INF/spring-mvc.xml` - Service/DAO 层配置:通过 `classpath:*` 加载(示例中未展示)[^3] - Quartz 定时任务:`quartz-config.xml` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值