`

Spring 3.x企业应用开发实战(5)----资源加载器

阅读更多

Spring定义了一套资源加载接口,并提供了实现类,ResourceLoader接口仅有一个getResource(String location)方法,这里的location不支持Ant风格的资源路径表达式。

ResourcePatternResolver扩展ResourceLoader接口,定义了一个新的接口方法:getResources(String locationPattern)//这里支持Ant风格。

PathMatchingResourcePatternResolver是Spring提供的标准实现类

实例:

 

  1. package com.techman.resource;  
  2.   
  3. import org.springframework.core.io.Resource;  
  4. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;  
  5. import org.springframework.core.io.support.ResourcePatternResolver;  
  6.   
  7. public class PatternResolerTest   
  8. {  
  9.     public static void main(String []args)throws Throwable  
  10.     {  
  11.         //实例化资源加载器接口  
  12.         ResourcePatternResolver rpr=new PathMatchingResourcePatternResolver();//是Spring提供的标准实现类  
  13.           
  14.         //加载所有类包com.techman(及子孙包)下以xml为后缀的文件  
  15.         Resource resources[]=rpr.getResources("classpath*:com/techman/**/*.xml");  
  16.           
  17.         for(Resource resource:resources)  
  18.         {  
  19.             System.out.println(resource.getDescription());   
  20.         }  
  21.     }  
  22. }  

 

 

7
11
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics