Spring加载资源地址前缀的classpath:和classpath*:的比较

来源:互联网 发布:淘宝5金冠店铺有哪些 编辑:程序博客网 时间:2024/05/22 14:28

spring支持哪些资源类型的地址前缀:

地址前缀

示 例

对应资源类型

classpath:classpath:com/coder/beanfactory/beans.xml从类路径中加载资源,classpath:和classpath:/是等价的,都是相对于类的根路径。资源文件可以在标准的文件系统中,也可以在jar或zip的类包中file:file:/conf/com/coder/beanfactory/beans.xml使用UrlResource从文件系统目录中装载资源,可采用绝对或相对路径http://http: //www.coder.xxx/resource/beans.xml使用UrlResource从Web服务器中装载资源ftp://ftp: //www.coder.xxx/resource/beans.xml使用UrlResource从FTP服务器中装载资源没有前缀com/coder/beanfactory/beans.xml根据ApplicationContext具体实现类采用对应的类型的Resource

其中和"classpath:"对应的,还有另一种比较难理解的"classpath*:"前缀。假设有多个JAR包或文件系统类路径都拥有一个相同的包名(如com.coder)。"classpath:"只会在第一个加载的com.coder包下查找,而"classpath*:"会到扫描所有这些JAR包及类路径下出现的com.coder类路径。
Ant风格资源地址支持3种匹配符:

● ?:匹配文件名中的一个字符;
● *:匹配文件名中任意个字符;
● **:匹配多层路径。

下面是几个Ant风格的资源路径的示例:

● classpath:com/t?st.xml :匹配com类路径下com/test.xml,com/tast.xml或者com/txst.xml;
● file:D:/conf/*.xml:匹配文件系统D:/conf目录下所有以xml为后缀的文件;
● classpath:com/**/test.xml:匹配com类路径下(当前目录及其子孙目录)的test.xml文件;
● classpath:org/springframework/**/*.xml:匹配类路径org/springframework下所有以xml为后缀的文件;
● classpath:org/**/servlet/bla.xml:匹配类路径org/springframework/servlet/bla.xml,也匹配org/springframework/testing/servlet/bla.xml,还匹配org/servlet/bla.xml。


阅读全文
0 0
原创粉丝点击