Spring对不同来源的Resources的支持

在讲解如何支持之前,有必要先说明白对Spring而言,Resources是什么以及如何处理不当会发生什么问题;

好的,先说Resources是什么?大家都知道Spring容器在启动的时候会加载各种各样的配置文件,这些个用于不同目的的配置文件,就可以称之为Resources(至于中文翻译成什么,个人一直没有合适的选择),而且这些配置文件可能来源某个URL地址,项目的类路径,某个文件系统,ServletContext环境中,甚至来源于二进制文件以及某些其它的输入流,那么由于Resources的来源这么广泛,一旦处理来源不当,就会引发各种文件不存在或找不到异常,比如今天在论坛里面看到一个小伙伴的这个异常,部分异常信息如下:

 

[html] view plain copy

  1. org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [springmvc.xml]; nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be opened because it does not exist  

 

JDK只提供了如java.net.URL、File等,缺少从类路径或者Web容器上下文中获取资源的操作类等,正因为如此,Spring框架提供了一个更为可靠的接口Resource用来抽象规范各种来源的资源,接口代码如下:

 

[html] view plain copy

  1. public interface Resource extends InputStreamSource {  
  2.       
  3.     boolean exists();  
  4.   
  5.     boolean isReadable();  
  6.   
  7.     boolean isOpen();  
  8.   
  9.     URL getURL() throws IOException;  
  10.   
  11.     URI getURI() throws IOException;  
  12.   
  13.     File getFile() throws IOException;  
  14.   
  15.     long contentLength() throws IOException;  
  16.   
  17.     long lastModified() throws IOException;  
  18.   
  19.     Resource createRelative(String relativePath) throws IOException;  
  20.   
  21.     String getFilename();  
  22.   
  23.     String getDescription();  
  24.   
  25. }  

[html] view plain copy

  1. public interface InputStreamSource {  
  2.   
  3.     InputStream getInputStream() throws IOException;  
  4.   
  5. }  

Spring框架提供了大量的对上述Resourse接口的实现类,来满足不同来源的Resource

1、UrlResource:这个类里面包装了java.net.URL,可以用来访问任何可以通过URL访问的对象,比如各类文件、HTTP目标以及FTP目标对象等;

2、ClassPathResource:该类代表了可以从类路径下面获取的Resource;

3、FileSystemResource:该类是java.io.File类处理的Resource实现,显然它可以支持File和URL;

4、ServletContextResource:该类是ServletContext资源的实现,它负责以相对于Web应用根目录的路径加载资源;

5、InputStreamResource:对应一个InputStream资源;

6、ByteArrayResource:二进制数组表示的资源,二进制数组资源可以在内存中通过程序构造;

和ByteArrayResource:这两者用的较少,只有在即特殊场合下才使用,就不做叙述;

 

既然有这么多种资源,访问不同类型的资源,必须使用相应的Resource实现类,这是比较麻烦的,对此Spring框架提供了一个ResourceLoader接口,该接口代码如下:

 

[html] view plain copy

  1. public interface ResourceLoader {  
  2.   
  3.     /** Pseudo URL prefix for loading from the class path: "classpath:" */  
  4.     String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;  
  5.   
  6.     Resource getResource(String location);  
  7.   
  8.     ClassLoader getClassLoader();  
  9.   
  10. }  

它可以根据一个资源地址加载文件资源,那么这个资源地址是什么格式的呢?以下是从某本书上的截图


这个时候,我们会发现只要是前缀指定好,Spring会解析这个资源地址,自动去指定地方加载资源,这就要求我们在指定资源路径的时候一定确定好想要加载的资源到底是什么类型的资源,否则就会报各种找不到的异常,这样一来就解决了咱们在文章开头提到的问题。

需要说明的是,比如与"classpath:"对应的,还有一种"classpath*:"的前缀;假设有多个jar包或文件系统类路径都拥有一个相同的包名(如com.springframewok)前者只会加载第一个加载的com.springframework包下查找,而后者会扫描所有的这些jar包和文件系统类路径下面的内容;

上述ResourceLoader接口只支持带资源类型前缀的表达式,不支持Ant风格的资源路径表达式,ResourcePaternResolver扩展了ResourceLoader接口,

 

[html] view plain copy

  1. public interface ResourcePatternResolver extends ResourceLoader {  
  2.   
  3.     /**  
  4.      * Pseudo URL prefix for all matching resources from the class path: "classpath*:"  
  5.      * This differs from ResourceLoader's classpath URL prefix in that it  
  6.      * retrieves all matching resources for a given name (e.g. "/beans.xml"),  
  7.      * for example in the root of all deployed JAR files.  
  8.      * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX  
  9.      */  
  10.     String CLASSPATH_ALL_URL_PREFIX = "classpath*:";  
  11.   
  12.     /**  
  13.      * Resolve the given location pattern into Resource objects.  
  14.      * <p>Overlapping resource entries that point to the same physical  
  15.      * resource should be avoided, as far as possible. The result should  
  16.      * have set semantics.  
  17.      * @param locationPattern the location pattern to resolve  
  18.      * @return the corresponding Resource objects  
  19.      * @throws IOException in case of I/O errors  
  20.      */  
  21.     Resource[] getResources(String locationPattern) throws IOException;  
  22.   
  23. }  

该接口定义了一个新的接口方法:getResources(String locationPattern),该方法支持带资源类型前缀及Ant风格的资源路径表达式,由于Spring所有的ApplicationContext实现类都直接或间接地实现了该接口,所以我们可以在Spring配置文件中方式使用。Ant风格的资源地址,支持3种匹配符;

 

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

 

下面是几个示例:

 

  • classpath:com/ssh/t?st.xml:匹配com/ssh路径下的com/ssh/test.xml、com/ssh/tast.xml等
  • file:D:/conf/*Config.xml:匹配文件系统D:/conf路径下的所有文件以Config.xml为后缀的文件,比如匹配HibernateConf.xml,SpringConf.xml等;
  • classpath:com/**/test/xml:匹配com类路径下(当前目录及其子孙目录)的test.xml文件,比如匹配:com/a/test.xml、com/a/b/c/d/test.xml等

 

只要是理解好了3种匹配符的作用,后续地使用没有问题,通过上面的说明,我们可以得出:只要是在Spring管理的环境配置中,可以放心大胆的按照上述规则制定各种资源文件

转载于:https://my.oschina.net/u/3776411/blog/1617165

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值