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 接口继承自 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接口,灵活的使用各类资源,这样屏蔽了实现,系统的兼容性更好。