org.springframework.core.io
Class ClassPathResource

java.lang.Object
  extended byorg.springframework.core.io.AbstractResource
      extended byorg.springframework.core.io.ClassPathResource
All Implemented Interfaces:
InputStreamSource, Resource

public class ClassPathResource
extends AbstractResource

Resource implementation for class path resources. Uses either the thread context class loader, a given ClassLoader or a given Class for loading resources.

Supports resolution as File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

Since:
28.12.2003
Author:
Juergen Hoeller
See Also:
Thread.getContextClassLoader(), ClassLoader.getResourceAsStream(java.lang.String), Class.getResourceAsStream(java.lang.String)

Field Summary
 
Fields inherited from class org.springframework.core.io.AbstractResource
URL_PROTOCOL_FILE
 
Constructor Summary
ClassPathResource(java.lang.String path)
          Create a new ClassPathResource for ClassLoader usage.
ClassPathResource(java.lang.String path, java.lang.Class clazz)
          Create a new ClassPathResource for Class usage.
ClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader)
          Create a new ClassPathResource for ClassLoader usage.
 
Method Summary
 Resource createRelative(java.lang.String relativePath)
          This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.
 boolean equals(java.lang.Object obj)
          This implementation compares description strings.
 java.lang.String getDescription()
          Return a description for this resource, to be used for error output when working with the resource.
 java.io.File getFile()
          This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.
 java.lang.String getFilename()
          This implementation always throws IllegalStateException, assuming that the resource does not carry a filename.
 java.io.InputStream getInputStream()
          Return an InputStream.
 java.net.URL getURL()
          This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.
 int hashCode()
          This implementation returns the description's hash code.
 
Methods inherited from class org.springframework.core.io.AbstractResource
exists, isOpen, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassPathResource

public ClassPathResource(java.lang.String path)
Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.

The thread context class loader will be used for loading the resource.

Parameters:
path - the absolute path within the classpath
See Also:
ClassLoader.getResourceAsStream(java.lang.String), Thread.getContextClassLoader()

ClassPathResource

public ClassPathResource(java.lang.String path,
                         java.lang.ClassLoader classLoader)
Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.

Parameters:
path - the absolute path within the classpath
classLoader - the class loader to load the resource with
See Also:
ClassLoader.getResourceAsStream(java.lang.String)

ClassPathResource

public ClassPathResource(java.lang.String path,
                         java.lang.Class clazz)
Create a new ClassPathResource for Class usage. The path can be relative to the given class, or absolute within the classpath via a leading slash.

Parameters:
path - relative or absolute path within the classpath
clazz - the class to load resources with
See Also:
Class.getResourceAsStream(java.lang.String)
Method Detail

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Description copied from interface: InputStreamSource
Return an InputStream. It is expected that each call creates a fresh stream.

Throws:
java.io.IOException - if the stream could not be opened

getURL

public java.net.URL getURL()
                    throws java.io.IOException
Description copied from class: AbstractResource
This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.

Specified by:
getURL in interface Resource
Overrides:
getURL in class AbstractResource
Throws:
java.io.IOException

getFile

public java.io.File getFile()
                     throws java.io.IOException
Description copied from class: AbstractResource
This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.

Specified by:
getFile in interface Resource
Overrides:
getFile in class AbstractResource
Throws:
java.io.IOException

createRelative

public Resource createRelative(java.lang.String relativePath)
Description copied from class: AbstractResource
This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.

Specified by:
createRelative in interface Resource
Overrides:
createRelative in class AbstractResource

getFilename

public java.lang.String getFilename()
Description copied from class: AbstractResource
This implementation always throws IllegalStateException, assuming that the resource does not carry a filename.

Specified by:
getFilename in interface Resource
Overrides:
getFilename in class AbstractResource

getDescription

public java.lang.String getDescription()
Description copied from interface: Resource
Return a description for this resource, to be used for error output when working with the resource.

Implementations are also encouraged to return this value from their toString method.

See Also:
Object.toString()

equals

public boolean equals(java.lang.Object obj)
Description copied from class: AbstractResource
This implementation compares description strings.

Overrides:
equals in class AbstractResource
See Also:
Resource.getDescription()

hashCode

public int hashCode()
Description copied from class: AbstractResource
This implementation returns the description's hash code.

Overrides:
hashCode in class AbstractResource
See Also:
Resource.getDescription()


Copyright (C) 2003-2004 The Spring Framework Project.