spring Resource接口

簡述

Interface for a resource descriptor that abstracts from the actual type of underlying resource, such as a file or class path resource.java

上面是Resource接口的doc描述,很明確的定義了接口的職責。它是資源的抽象,爲應用提供了統一的訪問api。 spring

resource 接口方法

Resource 接口繼承自 InputStreamSource ,接口中定了了不少方法,經過方法名能夠很直觀的知道方法的功能。api

`` public interface Resource extends InputStreamSource {ide

/**
 * Return whether this resource actually exists in physical form.
 * <p>This method performs a definitive existence check, whereas the
 * existence of a {[@code](https://my.oschina.net/codeo) Resource} handle only guarantees a
 * valid descriptor handle.
 */
boolean exists();

/**
 * Return whether the contents of this resource can be read,
 * e.g. via {[@link](https://my.oschina.net/u/393) #getInputStream()} or {[@link](https://my.oschina.net/u/393) #getFile()}.
 * <p>Will be {[@code](https://my.oschina.net/codeo) true} for typical resource descriptors;
 * note that actual content reading may still fail when attempted.
 * However, a value of {[@code](https://my.oschina.net/codeo) false} is a definitive indication
 * that the resource content cannot be read.
 * @see #getInputStream()
 */
boolean isReadable();

/**
 * 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 {@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
 */
URI getURI() 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
 */
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();

}ui

InputStreamSource 中只有一個方法,返回輸入流,這個方法不是全部的子類都實現了,後面我會舉例說明。

public interface InputStreamSource {this

/**
 * Return an {@link InputStream}.
 * <p>It is expected that each call creates a <i>fresh</i> stream.
 * <p>This requirement is particularly important when you consider an API such
 * as JavaMail, which needs to be able to read the stream multiple times when
 * creating mail attachments. For such a use case, it is <i>required</i>
 * that each {@code getInputStream()} call returns a fresh stream.
 * @return the input stream for the underlying resource (must not be {@code null})
 * @throws IOException if the stream could not be opened
 * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
 */
InputStream getInputStream() throws IOException;

}spa

## 舉例

spring Resource 有不少實現,下面描述幾個有表明性的實現。

### AbstractResource

是一個模板模式的抽象類,實現了 exists , isReadable , isOpen , getURI ,contentLength, 等方法,
有些有些方法是空實現,如getFilename,createRelative,getFile,這些方法須要子類按需實現。

### ClassPathResource

用來加載classpath中的資源,spring在初始化的時候回使用這個類來加載xml配置文件。

public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent) throws BeansException { super(parent); Assert.notNull(paths, "Path array must not be null"); Assert.notNull(clazz, "Class argument must not be null"); this.configResources = new Resource[paths.length]; for (int i = 0; i < paths.length; i++) { this.configResources[i] = new ClassPathResource(paths[i], clazz); } refresh(); }.net

### FileSystemResource

實現了 WritableResource 接口,該實現由寫的能力。支持系統文件資源

FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");code

### UrlResource

能夠表示URl和file資源。

new UrlResource("http://localhost:8080")orm

# 小結

咱們能夠在項目中使用Resource接口,靈活的使用各類資源,這樣屏蔽了實現,系統的兼容性更好。
相關文章
相關標籤/搜索