一)如何使用spring中的resource
Spring的资源文件访问功能使用起来十分简单,调用ApplicationContext.getResource的方法即可:
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
  2. Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
  3. Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt");
  4. Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");


二)resource的深入了解
其实知道了以上的gerResource方法,就已经能够满足我们日常的资源文件访问的需要了。但本着精益求精的精神,我们不妨看下spring的源码,看看spring到底是如何实现这个功能模块的。首先是接口Resource:
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. public interface Resource extends InputStreamSource {
  2. boolean exists();
  3. boolean isOpen();
  4. URL getURL() throws IOException;
  5. File getFile() throws IOException;
  6. Resource createRelative(String relativePath) throws IOException;
  7. String getFilename();
  8. String getDescription();
  9. }
  10. public interface InputStreamSource {
  11. InputStream getInputStream() throws IOException;
  12. }

基于这个底层接口,spring运用多态继承了一系列的子类以满足不同形式的资源文件访问:
UrlResource: file: http: ftp: 的资源访问形式都是它处理啦
ClassPathResource: 看名字也猜的出来classpath:的资源访问形式就是它处理的
FileSystemResource: 从文件系统路径获取资源文件,C:\XXXXX
ServletContextResource: web应用中从context下获取资源文件,相对路径绝对路径均可
InputStreamResource: 缺省的Resource类,如果根据输入的url形式找不到对应的Resource,那么就调用它了...当然我们也可以显示的调用它:
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. @Test
  2. public void testInputStreamResource() {
  3. ByteArrayInputStream bis = new ByteArrayInputStream("Hello World!".getBytes());
  4. Resource resource = new InputStreamResource(bis);
  5. if(resource.exists()) { ...... }
  6. }


ByteArrayResource: 看代码便知...
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. @Test
  2. public void testByteArrayResource() {
  3. Resource resource = new ByteArrayResource("Hello World!".getBytes());
  4. if(resource.exists()) { ...... }
  5. }


三)ResourceLoaderAware
和其它Aware一样,继承此接口后,当此bean实例化时会自动调用setResourceLoader(ResourceLoader resourceLoader)方法。
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. public interface ResourceLoaderAware {
  2. void setResourceLoader(ResourceLoader resourceLoader);
  3. }


四) Resource的注入
那么,如果是一个resouce类,Spring在配置文件中改如何处理以使它自动注入呢?样例如下:
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. <bean id="myBean" class="...">
  2. <property name="template" value="some/resource/path/myTemplate.txt"/>
  3. </bean>
  4. <property name="template" value="classpath:some/resource/path/myTemplate.txt">
  5. <property name="template" value="file:/some/resource/path/myTemplate.txt"/>

十分方便吧~~

五)资源文件的通配样式
当你需要一次获取多个资源文件时,你可以考虑采用以下一些通配的手段:
Java代码 Spring --- Resource _如何 Spring --- Resource _spring_02
  1. /WEB-INF/*-context.xml
  2. com/mycompany/**/applicationContext.xml
  3. file:C:/some/path/*-context.xml
  4. classpath:com/mycompany/**/applicationContext.xml
  5. classpath*:conf/appContext.xml //获取classpath内所有的conf/appContext.xml,不加*的话只会加载搜索时遇到的第一个匹配文件