Package org.springframework.core.io
Class ClassPathResource
- java.lang.Object
- org.springframework.core.io.AbstractResource
- org.springframework.core.io.AbstractFileResolvingResource
- org.springframework.core.io.ClassPathResource
- All Implemented Interfaces:
InputStreamSource,Resource
- Direct Known Subclasses:
DefaultResourceLoader.ClassPathContextResource
public class ClassPathResource extends AbstractFileResolvingResource
Resourceimplementation for class path resources. Uses either a givenClassLoaderor a givenClassfor loading resources.Supports resolution as
java.io.Fileif 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, Sam Brannen
- See Also:
ClassLoader.getResourceAsStream(String),Class.getResourceAsStream(String)
Constructor Summary
Constructors Modifier Constructor Description ClassPathResource(String path)Create a newClassPathResourceforClassLoaderusage.ClassPathResource(String path, Class<?> clazz)Create a newClassPathResourceforClassusage.ClassPathResource(String path, ClassLoader classLoader)Create a newClassPathResourceforClassLoaderusage.protectedClassPathResource(String path, ClassLoader classLoader, Class<?> clazz)Create a newClassPathResourcewith optionalClassLoaderandClass.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourcecreateRelative(String relativePath)This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.booleanequals(Object obj)This implementation compares the underlying class path locations.booleanexists()This implementation checks for the resolution of a resource URL.ClassLoadergetClassLoader()Return the ClassLoader that this resource will be obtained from.StringgetDescription()This implementation returns a description that includes the class path location.StringgetFilename()This implementation returns the name of the file that this class path resource refers to.InputStreamgetInputStream()This implementation opens an InputStream for the given class path resource.StringgetPath()Return the path for this resource (as resource path within the class path).URLgetURL()This implementation returns a URL for the underlying class path resource, if available.inthashCode()This implementation returns the hash code of the underlying class path location.protected URLresolveURL()Resolves a URL for the underlying class path resource.Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource
contentLength, customizeConnection, customizeConnection, getFile, getFile, getFileForLastModifiedCheck, isReadable, lastModified
Methods inherited from class org.springframework.core.io.AbstractResource
getURI, isOpen, toString
Constructor Detail
ClassPathResource
public ClassPathResource(String path)
Create a newClassPathResourceforClassLoaderusage. 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 class path- See Also:
ClassLoader.getResourceAsStream(String),ClassUtils.getDefaultClassLoader()
ClassPathResource
public ClassPathResource(String path, ClassLoader classLoader)
Create a newClassPathResourceforClassLoaderusage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.- Parameters:
path- the absolute path within the classpathclassLoader- the class loader to load the resource with, ornullfor the thread context class loader- See Also:
ClassLoader.getResourceAsStream(String)
ClassPathResource
public ClassPathResource(String path, Class<?> clazz)
Create a newClassPathResourceforClassusage. 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 class pathclazz- the class to load resources with- See Also:
Class.getResourceAsStream(java.lang.String)
ClassPathResource
protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz)
Create a newClassPathResourcewith optionalClassLoaderandClass. Only for internal usage.- Parameters:
path- relative or absolute path within the classpathclassLoader- the class loader to load the resource with, if anyclazz- the class to load resources with, if any
Method Detail
getPath
public final String getPath()
Return the path for this resource (as resource path within the class path).
getClassLoader
public final ClassLoader getClassLoader()
Return the ClassLoader that this resource will be obtained from.
exists
public boolean exists()
This implementation checks for the resolution of a resource URL.- Specified by:
existsin interfaceResource- Overrides:
existsin classAbstractFileResolvingResource- See Also:
ClassLoader.getResource(String),Class.getResource(String)
resolveURL
protected URL resolveURL()
Resolves a URL for the underlying class path resource.- Returns:
- the resolved URL, or
nullif not resolvable
getInputStream
public InputStream getInputStream() throws IOException
This implementation opens an InputStream for the given class path resource.- Returns:
- the input stream for the underlying resource (must not be
null) - Throws:
FileNotFoundException- if the underlying resource doesn't existIOException- if the content stream could not be opened- See Also:
ClassLoader.getResourceAsStream(String),Class.getResourceAsStream(String)
getURL
public URL getURL() throws IOException
This implementation returns a URL for the underlying class path resource, if available.- Specified by:
getURLin interfaceResource- Overrides:
getURLin classAbstractResource- Throws:
IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor- See Also:
ClassLoader.getResource(String),Class.getResource(String)
createRelative
public Resource createRelative(String relativePath)
This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.- Specified by:
createRelativein interfaceResource- Overrides:
createRelativein classAbstractResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- See Also:
StringUtils.applyRelativePath(String, String)
getFilename
public String getFilename()
This implementation returns the name of the file that this class path resource refers to.- Specified by:
getFilenamein interfaceResource- Overrides:
getFilenamein classAbstractResource- See Also:
StringUtils.getFilename(String)
getDescription
public String getDescription()
This implementation returns a description that includes the class path location.- See Also:
Object.toString()
equals
public boolean equals(Object obj)
This implementation compares the underlying class path locations.- Overrides:
equalsin classAbstractResource- See Also:
Resource.getDescription()
hashCode
public int hashCode()
This implementation returns the hash code of the underlying class path location.- Overrides:
hashCodein classAbstractResource- See Also:
Resource.getDescription()