Spring's Resource
interface is meant
to be a more capable interface for abstracting access to low-level
resources.
public interface Resource extends InputStreamSource { boolean exists(); boolean isOpen(); URL getURL() throws IOException; File getFile() throws IOException; Resource createRelative(String relativePath) throws IOException; String getFilename(); String getDescription(); }
public interface InputStreamSource { InputStream getInputStream() throws IOException; }
Some of the most important methods from the
Resource
interface are:
getInputStream()
: locates and opens the
resource, returning an InputStream
for reading
from the resource. It is expected that each invocation returns a
fresh InputStream
. It is the responsibility of
the caller to close the stream.
exists()
: returns a
boolean
indicating whether this resource actually
exists in physical form.
isOpen()
: returns a
boolean
indicating whether this resource represents
a handle with an open stream. If true
, the
InputStream
cannot be read multiple times, and
must be read once only and then closed to avoid resource leaks. Will
be false
for all usual resource implementations,
with the exception of
InputStreamResource
.
getDescription()
: returns a description
for this resource, to be used for error output when working with the
resource. This is often the fully qualified file name or the actual
URL of the resource.
Other methods allow you to obtain an actual
URL
or File
object
representing the resource (if the underlying implementation is compatible,
and supports that functionality).
The Resource
abstraction is used
extensively in Spring itself, as an argument type in many method
signatures when a resource is needed. Other methods in some Spring APIs
(such as the constructors to various
ApplicationContext
implementations), take a
String
which in unadorned or simple form is used to
create a Resource
appropriate to that
context implementation, or via special prefixes on the
String
path, allow the caller to specify that a
specific Resource
implementation must be
created and used.
While the Resource
interface is used
a lot with Spring and by Spring, it's actually very useful to use as a
general utility class by itself in your own code, for access to resources,
even when your code doesn't know or care about any other parts of Spring.
While this couples your code to Spring, it really only couples it to this
small set of utility classes, which are serving as a more capable
replacement for URL
, and can be considered
equivalent to any other library you would use for this purpose.
It is important to note that the
Resource
abstraction does not replace
functionality: it wraps it where possible. For example, a
UrlResource
wraps a URL, and uses the wrapped
URL
to do its work.