Spring源码学习-Resource

Spring的Resource接口为资源访问提供了统一的接口,不同的实现类实现了从不同上下文获取资源。下面是该接口的方法:

public interface Resource extends InputStreamSource {

	/**
	 * Determine whether this resource actually exists in physical form.
	 * <p>This method performs a definitive existence check, whereas the
	 * existence of a {@code Resource} handle only guarantees a valid
	 * descriptor handle.
	 */
	boolean exists();

	/**
	 * Indicate whether the contents of this resource can be read via
	 * {@link #getInputStream()}.
	 * <p>Will be {@code true} for typical resource descriptors;
	 * note that actual content reading may still fail when attempted.
	 * However, a value of {@code false} is a definitive indication
	 * that the resource content cannot be read.
	 * @see #getInputStream()
	 */
	boolean isReadable();

	/**
	 * Indicate whether this resource represents a handle with an open stream.
	 * If {@code true}, the InputStream cannot be read multiple times,
	 * and must be read and closed to avoid resource leaks.
	 * <p>Will be {@code false} for typical resource descriptors.
	 */
	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 getURL() throws IOException;

	/**
	 * Return a URI handle for this resource.
	 * @throws IOException if the resource cannot be resolved as URI,
	 * i.e. if the resource is not available as descriptor
	 * @since 2.5
	 */
	URI getURI() throws IOException;

	/**
	 * Return a File handle for this resource.
	 * @throws java.io.FileNotFoundException if the resource cannot be resolved as
	 * absolute file path, i.e. if the resource is not available in a file system
	 * @throws IOException in case of general resolution/reading failures
	 * @see #getInputStream()
	 */
	File getFile() throws IOException;

	/**
	 * Determine the content length for this resource.
	 * @throws IOException if the resource cannot be resolved
	 * (in the file system or as some other known physical resource type)
	 */
	long contentLength() throws IOException;

	/**
	 * Determine the last-modified timestamp for this resource.
	 * @throws IOException if the resource cannot be resolved
	 * (in the file system or as some other known physical resource type)
	 */
	long lastModified() throws IOException;

	/**
	 * Create a resource relative to this resource.
	 * @param relativePath the relative path (relative to this resource)
	 * @return the resource handle for the relative resource
	 * @throws IOException if the relative resource cannot be determined
	 */
	Resource createRelative(String relativePath) throws IOException;

	/**
	 * Determine a filename for this resource, i.e. typically the last
	 * part of the path: for example, "myfile.txt".
	 * <p>Returns {@code null} if this type of resource does not
	 * have a filename.
	 */
	String getFilename();

	/**
	 * 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 {@code toString} method.
	 * @see Object#toString()
	 */
	String getDescription();

}

下面列举几个常用的Resource实现类。

  • AbstractResource提供了Resource的默认实现;
  • UrlResource封装了一个java.net.URL对象,用来访问URL可以正常访问的任意对象,比如文件;
  • 可以使用ClassPathResource来获取类路径上的资源ClassPathResource可以使用线程上下文的加载器、调用者提供的加载器或指定的类中的任意一个来加载资源;
  • FileSystemResource是针对java.io.File提供的Resource实现;
  • ServletContextResource为了获取web根路径的ServletContext资源而提供的Resource实现;
  • 在确实没有找到其他合适的Resource实现时,才使用InputSteamResource;
  • 当需要从字节数组加载内容时,ByteArrayResource是一个不错的选择,使用ByteArrayResource可以不用求助于InputStreamResource;

相关推荐

  1. Spring学习-Resource

    2024-06-14 10:48:06       8 阅读
  2. spring学习第一课

    2024-06-14 10:48:06       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-14 10:48:06       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-14 10:48:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-14 10:48:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-14 10:48:06       18 阅读

热门阅读

  1. 行列视(RCV)能解决哪些问题?

    2024-06-14 10:48:06       8 阅读
  2. easyExcel导入日期LocalDateTime等类型不匹配问题

    2024-06-14 10:48:06       8 阅读
  3. milvus的磁盘索引

    2024-06-14 10:48:06       8 阅读
  4. npm发布自己的插件包

    2024-06-14 10:48:06       8 阅读
  5. redis的分布式session和本地的session有啥区别

    2024-06-14 10:48:06       9 阅读
  6. postgresql中geometry类型数据迁移

    2024-06-14 10:48:06       9 阅读
  7. 小程序的价值是什么?

    2024-06-14 10:48:06       10 阅读
  8. 树的经典问题和方法

    2024-06-14 10:48:06       7 阅读
  9. 记录一次网络延迟的事件分析

    2024-06-14 10:48:06       10 阅读