Spring之所以抽象Resource接口,是因为传统的java使用URL和标准的handler来处置资源。但是有个局限是不能直接读取classpath下的资源。因此Spring定义了一套接口。这套接口分为两部分,Resource和ResourceLoader,其中是对资源的抽象,后者负责加载资源。Spring的ApplicationContext实现了ResourceLoader接口。
Resource
首先看Resource的接口定义。
public interface Resource extends InputStreamSource {
boolean exists();
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