Spring-web源代码阅读之Resource

未分类

2022-07-22

277

0

资源 ,通俗的理解就是有用的东西。在spring里使用Resource接口来对它进行描述,用Resource这个接口来代表一个真实的资源类型,例如:文件或者类路径中的图片等等。

Resource的父类是InputStreamSource,父类中只有一个方法getInputStream(),返回了一个java.io.InputStream对象并且每次都是一个新的Inputstream对象,这个父类说明了理论上所有它的子类都是可以用java.io.InputStreams处理的资源。

对于Resource的行为的规范,如下:

public interface Resource extends InputStreamSource {

	/**
	 * Return whether this resource actually exists in physical form.
         返回该资源是否物理上存在
	 */
	boolean exists();

	/**
	 * Return whether this resource represents a handle with an open
	 * stream. If true, the InputStream cannot be read multiple times,
	 * and must be read and closed to avoid resource leaks.
	 * <p>Will be false for all usual resource descriptors.
          返回该资源是否代表了一个打开的流的,如果是那这个输入流不能被多读多次,并且必须读完后关闭,避免资源泄露。对所有一般的(非流的)资源都返回false
	 */
	boolean isOpen();

	/**
	 * Return a URL handle for this resource.
	 * @throws IOException if the resource cannot be resolved as URL,
	 * i.e. if the resource is not available as descriptor
             返回该资源的URL对象,如果该资源不能被解析为url的话,抛出io异常,如果这个资源不可用,也会抛出io异常。
	 */
	URL getURL() throws IOException;

	/**
	 * Return a File handle for this resource.
	 * @throws IOException if the resource cannot be resolved as absolute
	 * file path, i.e. if the resource is not available in a file system
          返回代表该资源的一个文件对象,如果该资源不能被解析为绝对文件路径的话,抛出io异常,如果这个资源在文件系统中不可用,也会抛出io异常。
	 */
	File getFile() throws IOException;

	/**
	 * Return a description for this resource,
	 * to be used for error output when working with the resource.
	 * <p>Implementations are also encouraged to return this value
	 * from their toString method.
	 * @see java.lang.Object#toString
           返回该资源的描述,用于和该资源相关的错误输出,鼓励实现类在自己的toString方法中返回该描述。
	 */
	String getDescription();

}

 由上述代码可以看出,在spring1.0的时候,该接口的作者认为资源大概分为了两大类,一类是可以被解析为URL的,一类是就是文件。

发表评论

全部评论:0条

Eric

莫等青春散场,才后悔来不及、回不去、得不到

热评文章

推荐文章