在使用spring时,经常会看到类似 classpth:、classpath*: 这样的前缀,不管是加载spring xml配置文件还是其配置文件中加载资源文件都会看到这两种前缀配置,其实这两种前缀是有区别的,下面将举例详细解释。
[一]、测试项目准备
我们以spring中加载properties资源文件为例讲解,目录结构大致如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | src ├─main │ ├─filters │ │ │ ├─java │ │ └─com │ │ └─micmiu │ │ ├─demoweb │ │ │ │ .... │ │ │ │ │ │ │ └─utils │ │ │ │ │ └─modules │ │ │ ├─resources │ │ │ application.properties │ │ │ applicationContext-shiro.xml │ │ │ applicationContext.xml │ │ │ hibernate.cfg.xml │ │ │ log4j.properties │ │ │ spring-mvc.xml │ │ │ spring-view.xml │ │ │ └─webapp │ │ │ └─WEB-INF │ └─test ├─java │ └─com │ └─micmiu │ ├─demoweb │ │ TestOther.java │ └─resources application.properties |
同时 在该项目的lib中添加一个测试的micmiu-test.jar包,jar包中的文件结构如下:
1 2 3 4 5 6 7 8 9 10 11 | micmiu-test.jar │ application.properties │ ├─com │ └─micmiu │ └─test │ application.properties │ RunApp.class │ └─META-INF MANIFEST.MF |
从准备的测试环境中我们可以看到在不同目录下的四个同名的application.properties资源文件。
[二]、测试代码:TestClassPath.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.micmiu.demoweb;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
/** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class TestClassPath {
/** * @param args */ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:/applicationContext.xml"); System.out.println(ctx.getClassLoader().getResource("").getPath());
} } |
[三]、测试结果
spring配置文件:applicationContext.xml 中两种不同的properties文件加载配置:
第一种:classpath:
1 2 | <context:property-placeholder ignore-unresolvable="true" location="classpath:/application.properties" /> |
这种配置下运行测试代码,日志信息中有关加载properties资源文件只有一条 如下:
1 | Loading properties file from class path resource [application.properties] |
第二种: classpath*:
1 2 | <context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" /> |
这种配置下运行测试代码,日志信息中有关加载properties资源文件会有三条如下:
1 2 3 | Loading properties file from URL [file:/D:/workspace_sun/framework-dev/micmiu-demoweb/target/test-classes/application.properties] Loading properties file from URL [file:/D:/workspace_sun/framework-dev/micmiu-demoweb/target/classes/application.properties] Loading properties file from URL [jar:file:/D:/micmiu-test.jar!/application.properties] |
由此日志信息可知:
- 同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源
- classpath*:需要遍历所有的classpath,效率肯定比不上classpath,因此在项目设计的初期就尽量规划好资源文件所在的路径,避免使用classpath*来加载
分析到这,结合测试我们可以总结一下:
1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
3.文件名字包含通配符资源(如果spring-*.xml,spring*.xml), 如果根目录为"", classpath加载不到任何资源, 而classpath*则可以加载到classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源
第1,2点比较好表理解,大家可以自行测试,第三点表述有点绕,举个例,现在有资源文件结构如下:
classpath:notice*.txt 加载不到资源
classpath*:notice*.txt 加载到resource根目录下notice.txt
classpath:META-INF/notice*.txt 加载到META-INF下的一个资源(classpath是加载到匹配的第一个资源,就算删除classpath下的notice.txt,他仍然可以 加载jar包中的notice.txt)
classpath:META-*/notice*.txt 加载不到任何资源
classpath*:META-INF/notice*.txt 加载到classpath以及所有jar包中META-INF目录下以notice开头的txt文件
classpath*:META-*/notice*.txt 只能加载到classpath下 META-INF目录的notice.txt