Spring 资源加载(classpath)

前言

  Spring 提供了一个强大的加载资源的机制,仅通过资源地址的特殊标识就可以访问相应的资源,不但能通过“classpath”,“file”等资源地址前缀识别不同的资源类型,还支持Ant分格的带通配符的资源地址。

资源地址前缀

classpath:

实例:  classpath:resources/applicationContext.xml
说明:  从类路径中加载资源,classpath:和classpath:/等代价的,都是相对于类的根路径。资源文件可以在标准的文件系统中,也可以在JAR或ZIP的类包中。
补充:  在下面的maven项目中,资源路径
classpath:resources/applicationContext.xml指向spring的配置资源文件applicationContext.xml。

这里写图片描述

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:resources/applicationContext.xml");

file:

实例:  file:/conf/com/dmw/beanfactory/beans.xml
说明:  使用UrlResources从文件系统目录中装载资源,可采用绝对或相对路径。

http://

实例:  http://www.dmw.com/resource/beans.xml
说明:  使用UrlResource从Web服务器中装载资源。

ftp://

实例:  ftp://www.dmw.com/resource/beans.xml
说明:  使用UrlResource从FTP服务器中装载资源。

没有前缀

实例:  com/dmw/beanfactory/beans.xml
说明:  根据ApplicationContext的具体实现类采用对应类型的Resource。

Ant风格的资源地址

Ant 风格的资源地址支持3种匹配符。
?: 匹配文件名中的一个字符。
*: 匹配文件名中的任意字符。
**: 匹配多层路径。

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

猜你喜欢

转载自blog.csdn.net/dmw2016/article/details/79989449