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
Resource implementation for class path resources. Uses either a
 given ClassLoader or a given Class for loading resources.
 Supports resolution as java.io.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, Sam Brannen
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionClassPathResource(String path) Create a newClassPathResourceforClassLoaderusage.ClassPathResource(String path, Class<?> clazz) Create a newClassPathResourceforClassusage.ClassPathResource(String path, ClassLoader classLoader) Create a newClassPathResourceforClassLoaderusage.
- 
Method SummaryModifier and TypeMethodDescriptioncreateRelative(String relativePath) This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.booleanThis implementation compares the underlying class path locations.booleanexists()This implementation checks for the resolution of a resource URL.final ClassLoaderReturn the ClassLoader that this resource will be obtained from.This implementation returns a description that includes the class path location.This implementation returns the name of the file that this class path resource refers to.This implementation opens an InputStream for the given class path resource.final StringgetPath()Return the path for this resource (as resource path within the class path).getURL()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.booleanThis implementation checks for the resolution of a resource URL upfront, then proceeding withAbstractFileResolvingResource's length check.protected URLResolves a URL for the underlying class path resource.Methods inherited from class org.springframework.core.io.AbstractFileResolvingResourcecontentLength, customizeConnection, customizeConnection, getFile, getFile, getFileForLastModifiedCheck, isFile, isFile, lastModified, readableChannelMethods inherited from class org.springframework.core.io.AbstractResourcegetURI, isOpen, toString
- 
Constructor Details- 
ClassPathResourceCreate 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:
 
- 
ClassPathResourceCreate 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 classpath
- classLoader- the class loader to load the resource with, or- nullfor the thread context class loader
- See Also:
 
- 
ClassPathResourceCreate 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 path
- clazz- the class to load resources with
- See Also:
 
 
- 
- 
Method Details- 
getPathReturn the path for this resource (as resource path within the class path).
- 
getClassLoaderReturn the ClassLoader that this resource will be obtained from.
- 
existspublic boolean exists()This implementation checks for the resolution of a resource URL.- Specified by:
- existsin interface- Resource
- Overrides:
- existsin class- AbstractFileResolvingResource
- See Also:
 
- 
isReadablepublic boolean isReadable()This implementation checks for the resolution of a resource URL upfront, then proceeding withAbstractFileResolvingResource's length check.- Specified by:
- isReadablein interface- Resource
- Overrides:
- isReadablein class- AbstractFileResolvingResource
- See Also:
 
- 
resolveURLResolves a URL for the underlying class path resource.- Returns:
- the resolved URL, or nullif not resolvable
 
- 
getInputStreamThis 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 does not exist
- IOException- if the content stream could not be opened
- See Also:
 
- 
getURLThis implementation returns a URL for the underlying class path resource, if available.- Specified by:
- getURLin interface- Resource
- Overrides:
- getURLin class- AbstractResource
- Throws:
- IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor
- See Also:
 
- 
createRelativeThis implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.- Specified by:
- createRelativein interface- Resource
- Overrides:
- createRelativein class- AbstractResource
- Parameters:
- relativePath- the relative path (relative to this resource)
- Returns:
- the resource handle for the relative resource
- See Also:
 
- 
getFilenameThis implementation returns the name of the file that this class path resource refers to.- Specified by:
- getFilenamein interface- Resource
- Overrides:
- getFilenamein class- AbstractResource
- See Also:
 
- 
getDescriptionThis implementation returns a description that includes the class path location.- See Also:
 
- 
equalsThis implementation compares the underlying class path locations.- Overrides:
- equalsin class- AbstractResource
- See Also:
 
- 
hashCodepublic int hashCode()This implementation returns the hash code of the underlying class path location.- Overrides:
- hashCodein class- AbstractResource
- See Also:
 
 
-