`
perfy315
  • 浏览: 411908 次
社区版块
存档分类
最新评论

web.xml 配置中classpath: 与classpath*:的区别

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

解释classes含义:
1.存放各种资源配置文件 eg.init.properties log4j.properties struts.xml
2.存放模板文件 eg.actionerror.ftl
3.存放class文件 对应的是项目开发时的src目录编译文件
总结:这是一个定位资源的入口

如果你知道开发过程中有这么一句话:惯例大于配置 那么也许你会改变你的想法

对于第二个问题
这个涉及的是lib和classes下文件访问优先级的问题: lib>classes
对于性能的影响应该不在这个范畴

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

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

或者引用其子目录下的文件,如
<param-value>classpath:context/conf/controller.xml</param-value>


classpath*的使用:当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,则表示仅仅加载第一个classpath路径,代码片段:
<param-value>classpath*:context/conf/controller*.xml</param-value>


另外:
"**/" 表示的是任意目录;
"**/applicationContext-*.xml"  表示任意目录下的以"applicationContext-"开头的XML文件。 
程序部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 WEB-INF/classes目录下
classpath:与classpath*:的区别在于,

前者只会从第一个classpath中加载,而
后者会从所有的classpath中加载 

如果要加载的资源,
不在当前ClassLoader的路径里,那么用classpath:前缀是找不到的,
这种情况下就需要使用classpath*:前缀

在多个classpath中存在同名资源,都需要加载,
那么用classpath:只会加载第一个,这种情况下也需要用classpath*:前缀

注意:
用classpath*:需要遍历所有的classpath,所以加载速度是很慢的,因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。
分享到:
评论
4 楼 surpassno 2017-10-09  
实践下来,感觉有不一致的地方。
项目依赖
project-service 依赖于project-impl
层次结构
项目一
-project-service
--src
---main
----resources
-----applicationContext.xml(引用到project-impl项目中的config-mapper-member_info.xml 文件)

项目二
-project-impl
--src
---main
----resources
-----config-mapper-member_info.xml
运行 mvn clean install 后生成project-service 的target目录如下。
-project-service
--WEB-INF
---classes
----applicationContext.xml
---lib
----project-impl.jar
-----config-mapper-member_info.xml(解压后可看到)

按照楼主这意思“classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. ”在applicationContext.xml 引用config-mapper-member_info.xml 应该使用classpath*:, 但是实际操作使用classpath:,使用classpath*:反而报错找不到文件。请楼主大神解惑。
3 楼 GGGGeek 2016-12-17  
细节中的细节
2 楼 AKka 2015-06-05  
1 楼 弱水_穿云天 2014-11-27  
好!!!不错

相关推荐

    web.xml中如何设置配置文件的加载路径实例详解

    web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库、spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者...

    ssh框架在application.xml中配置数据源所需jar

    - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...

    企业人力资源管理项目SSH+EXT+MySQL+MD5

    在web.xml的display-name标签与welcome-file-list标签中加入一下代码 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context....

    asm5.0.jar

    Error creating bean with name 'sqlSessionFactory' defined in URL [file:/E:/cloudwave-core/src/main/webapp/WEB- INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception ...

    负载均衡软件

    软件负载均衡, 为没有部署硬件负载均衡器的应用提供负载均衡功能;已打包成war文件; 1. 系统提供完整的日志信息(log4j.properties); 2. 系统提供节点连通性检查开关和...5. web.xml配置文件位置在/WEB-INF/web.xml

    ehcahe页面缓存详细内容

    2.将ehcache.xml放入能加载到classpath中的任意包中,不能放入WEB-INF中(其详细配置参数代表含义已经注释) 3.在工程中web.xml配置过滤器 &lt;filter-name&gt;SimplePageCachingFilter &lt;filter-class&gt;...

    maven相关资料

    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml"); 二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别 ...

    基于java的企业级应用开发:Spring的核心容器.ppt

    通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; classpath:spring/applicationContext.xml ...

    基于java的企业级应用开发:Spring MVC的核心类和注解.ppt

    在使用时,只需将其配置在项目的web.xml文件中,其配置代码如下: &lt;servlet&gt; &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt; &lt;servlet-class&gt; org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt; ...

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: &lt;?xml...

    SSH第7章上机.zip ACCP8.0

    在web.xml中配置struts &lt;filter-name&gt;struts2 &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter &lt;filter-name&gt;struts2 &lt;url-pattern&gt;/* 在web.xml配置spring &lt;!-- ...

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。...- Find registry server-registry.xml at classpath resource - Server startup in 5329 ms

    Tomcat及其插件apache-tomcat-5.5.26-admin.zip的安装

    解压Administration Web Application,假设目录为F:\ apache-tomcat-5.5.25 把F:\ apache-tomcat-5.5.25 \server\webapps下面的admin文件夹copy到D:\apache-tomcat-5.5.25\server\webapps下面; 把F:\ apache-...

    DOS命令使用方法(超全).

    &lt;param-value&gt;classpath*:applicationContext-*.xml &lt;!-- 这是spring 监听 --&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener &lt;!-- 利用spring监听 编码设置 --&gt; ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.5.1. iBATIS 1.x和2.x的概览与区别 12.5.2. iBATIS SQL Maps 1.x 12.5.2.1. 创建SqlMap 12.5.2.2. 使用 SqlMapTemplate 和 SqlMapDaoSupport 12.5.3. iBATIS SQL Maps 2.x 12.5.3.1. 创建SqlMapClient 12.5.3.2....

    生活轨迹SSH服务端

    -- &lt;param-value&gt;/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml&lt;/param-value&gt; --&gt; &lt;param-value&gt;classpath:beans.xml &lt;filter-name&gt;struts2 &lt;filter-class&gt;org.apache.struts2...

    让Struts2可以从classpath及jar中查找jsp文件

    1、把两个类文件加入你的工程或者打包成jar。...4、注意:这个ResultType会在你的WEB-INF下面建立一个名为CLASSPATH-PAGES的文件夹,并将找到的jsp释放到这里。你可以改写这个类,从而在某个地方配置这一路径。

    客户关系管理系统框架搭建(二)

    &lt;value&gt;classpath:hibernate.cfg.xml &lt;!--4 创建事务管理器 aop切面--&gt; &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; ...

    Spring MVC Demo

    &lt;param-value&gt;classpath:applicationContext-*.xml &lt;!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行 &lt;param-value&gt;/WEB-INF/mvc.xml,/WEB-INF/mvc1.xml &lt;param-value&gt;/WEB-INF/mvc*.xml ...

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    这个简单的例子演示了在 web 应用程序中使用的 Spring 容器,即org.springframework.web.context.WebApplicationContext Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context...

Global site tag (gtag.js) - Google Analytics