Class 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 java.net.URL.
- since:
- 28.12.2003
- Author:
- Juergen Hoeller, sam Brannen
- see Also:
-
Constructor summary
ConstructorsConstructorDescriptionClassPathResource(string path) Create a newClassPathResourceforClassLoaderusage.ClassPathResource(string path, @Nullable Class<?> clazz) Create a newClassPathResourceforClassusage.ClassPathResource(string path, @Nullable ClassLoader classLoader) Create a newClassPathResourceforClassLoaderusage. -
Method summary
Modifier and TypeMethodDescriptioncreateRelative(string relativePath) This implementation creates aClassPathResource, applying the given path relative to the path used to create this descriptor.booleanThis implementation compares the underlying class path locations and associated class loaders.booleanexists()This implementation checks for the resolution of a resource URL.final @Nullable ClassLoaderReturn theClassLoaderthat this resource will be obtained from.This implementation returns a description that includes the absolute class path location.This implementation returns the name of the file that this class path resource refers to.This implementation opens anInputstreamfor the underlying class path resource, if available.final stringgetPath()Return the absolute path for this resource, as a cleaned 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.Resolves aURLfor the underlying class path resource.Methods inherited from class AbstractFileResolvingResource
contentLength, customizeConnection, customizeConnection, getFile, getFile, getFileForLastModifiedCheck, isFile, isFile, lastModified, readableChannelMethods inherited from class AbstractResource
getURI, isOpen, tostringMethods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface Resource
getContentAsByteArray, getContentAsstring, getFilePath
-
Constructor Details
-
ClassPathResource
Create a newClassPathResourceforClassLoaderusage.A leading slash will be removed, as the
ClassLoaderresource access methods will not accept it.The default class loader will be used for loading the resource.
- Parameters:
path- the absolute path within the class path- see Also:
-
ClassPathResource
Create a newClassPathResourceforClassLoaderusage.A leading slash will be removed, as the
ClassLoaderresource access methods will not accept it.If the supplied
ClassLoaderisnull, the default class loader will be used for loading the resource.- Parameters:
path- the absolute path within the class pathclassLoader- the class loader to load the resource with- see Also:
-
ClassPathResource
Create a newClassPathResourceforClassusage.The path can be relative to the given class, or absolute within the class path via a leading slash.
If the supplied
Classisnull, the default class loader will be used for loading the resource.This is also useful for resource access within the module system, loading a resource from the containing module of a given
Class. seeModuleResourceand its javadoc.- Parameters:
path- relative or absolute path within the class pathclazz- the class to load resources with- see Also:
-
-
Method Details
-
getPath
Return the absolute path for this resource, as a cleaned resource path within the class path.The path returned by this method does not have a leading slash and is suitable for use with
ClassLoader.getResource(string). -
getClassLoader
Return theClassLoaderthat this resource will be obtained from. -
exists
public boolean exists()This implementation checks for the resolution of a resource URL.- specified by:
exists in interface Resource- Overrides:
exists in class AbstractFileResolvingResource- see Also:
-
isReadable
public boolean isReadable()This implementation checks for the resolution of a resource URL upfront, then proceeding withAbstractFileResolvingResource's length check.- specified by:
isReadable in interface Resource- Overrides:
isReadable in class AbstractFileResolvingResource- see Also:
-
resolveURL
-
getInputstream
This implementation opens anInputstreamfor the underlying class path resource, if available.- Returns:
- the input stream for the underlying resource (must not be
null) - Throws:
IOException- if the content stream could not be opened- see Also:
-
getURL
This implementation returns a URL for the underlying class path resource, if available.- specified by:
getURL in interface Resource- Overrides:
getURL in 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:
-
createRelative
This implementation creates aClassPathResource, applying the given path relative to the path used to create this descriptor.- specified by:
createRelative in interface Resource- Overrides:
createRelative in class AbstractResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- see Also:
-
getFilename
This implementation returns the name of the file that this class path resource refers to.- specified by:
getFilename in interface Resource- Overrides:
getFilename in class AbstractResource- see Also:
-
getDescription
This implementation returns a description that includes the absolute class path location.- see Also:
-
equals
This implementation compares the underlying class path locations and associated class loaders.- Overrides:
equals in class AbstractResource- see Also:
-
hashCode
public int hashCode()This implementation returns the hash code of the underlying class path location.- Overrides:
hashCode in class AbstractResource- see Also:
-