spring源码分析------IO(1)

spring核心库之io

1.InputStreamSource

    顶层的接口,返回输入流                                  

package org.springframework.core.io;
import java.io.IOException;
import java.io.InputStream;

public interface InputStreamSource {
	InputStream getInputStream() throws IOException;
}

 

2.Resource

    继承了InputStreamSource接口,资源描述接口,用于抽象底层资源

package org.springframework.core.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
public interface Resource extends InputStreamSource {
	boolean exists();
	boolean isReadable();
	boolean isOpen();
	URL getURL() throws IOException;
	URI getURI() throws IOException;
	File getFile() throws IOException;
	long contentLength() throws IOException;
	long lastModified() throws IOException;
	Resource createRelative(String relativePath) throws IOException;
	String getFilename();
	String getDescription();
}

 

      Resource接口派生了一个个抽象类:  AbstractResource; 两个接口:ContextResource和WritableResource 。

      其中,AbstractResource是Resource接口最基本的实现, 返回一些默认值;ContextResource作为从上下文载入资源的扩展接口,例如从javax.servlet.ServletContext或者javax.portlet.PortletContext。

   

package org.springframework.core.io;

public interface ContextResource extends Resource {
     //返回相对于上下文的路径
     String getPathWithinContext();

}

 

package org.springframework.core.io;

import java.io.IOException;
import java.io.OutputStream;

public interface WritableResource extends Resource {

     boolean isWritable();

     OutputStream getOutputStream() throws IOException;
}

 3.AbstractResource子类

 

  1)AbstractFileResolvingResource是处理url资源的基类,这些url包括使用了jboss vfs协议的url。其派生类有:

    ClassPathResource:使用给定的classloader或者class加载资源。它的派生类是ClassPathContextResource显式的表达了一个和上下文相关的路径资源。另一个派生类  为ClassRelativeContextResource,两者差别之处就是前者使用了classloader作为构造函数参数实例化,而后者使用class作为构造函数参数。本质区别是相对路径和绝对  路径资源。

  • PortletContextResource:用于处理Portlet上下文的资源。
  • ServletContextResource:用于处理servlet上下文的资源。
  • UrlResource:处理URL定义的资源,以及支持使用“flie:”协议的资源。

   

  2)BeanDefinitionResource

  包装了BeanDefinition对象。

 

  3)ByteArrayResource

  从给定的字节数组加载文件

  4)DescriptiveResource

  作为标记使用,包装了一个资源,为其制定了描述

  5)FileSystemResource

  Resource针对java.io.File类型的包装实现,并且支持URL资源。子类:

  FileSystemContextResource通过实现ContextResource接口,显示的表示了一个上下文相对路径下的资源。

  6)InputStreamResource   将InputStream包装成资源类   7)VfsResource   VFS基于Resource的实现,支持JBOSS 5.X, 6.X, 7.X上的相关的VFS API版本    8)FileSystemResource   包装了一File对象作为资源  

猜你喜欢

转载自steven19880224.iteye.com/blog/1924155