public class ClassPathResource extends AbstractFileResolvingResource
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.
ClassLoader.getResourceAsStream(String)
,
Class.getResourceAsStream(String)
Modifier and Type | Field and Description |
---|---|
private java.lang.ClassLoader |
classLoader |
private java.lang.Class<?> |
clazz |
private java.lang.String |
path |
Modifier | Constructor and Description |
---|---|
|
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. |
protected |
ClassPathResource(java.lang.String path,
java.lang.ClassLoader classLoader,
java.lang.Class<?> clazz)
Create a new
ClassPathResource with optional ClassLoader
and Class . |
Modifier and Type | Method and Description |
---|---|
Resource |
createRelative(java.lang.String relativePath)
This implementation creates a ClassPathResource, applying the given path
relative to the path of the underlying resource of this descriptor.
|
boolean |
equals(java.lang.Object obj)
This implementation compares the underlying class path locations.
|
boolean |
exists()
This implementation checks for the resolution of a resource URL.
|
java.lang.ClassLoader |
getClassLoader()
Return the ClassLoader that this resource will be obtained from.
|
java.lang.String |
getDescription()
This implementation returns a description that includes the class path location.
|
java.lang.String |
getFilename()
This implementation returns the name of the file that this class path
resource refers to.
|
java.io.InputStream |
getInputStream()
This implementation opens an InputStream for the given class path resource.
|
java.lang.String |
getPath()
Return the path for this resource (as resource path within the class path).
|
java.net.URL |
getURL()
This implementation returns a URL for the underlying class path resource,
if available.
|
int |
hashCode()
This implementation returns the hash code of the underlying
class path location.
|
protected java.net.URL |
resolveURL()
Resolves a URL for the underlying class path resource.
|
contentLength, customizeConnection, customizeConnection, getFile, getFile, getFileForLastModifiedCheck, isReadable, lastModified
getURI, isOpen, toString
private final java.lang.String path
private java.lang.ClassLoader classLoader
private java.lang.Class<?> clazz
public ClassPathResource(java.lang.String path)
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.
path
- the absolute path within the class pathClassLoader.getResourceAsStream(String)
,
ClassUtils.getDefaultClassLoader()
public ClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader)
ClassPathResource
for ClassLoader
usage.
A leading slash will be removed, as the ClassLoader resource access
methods will not accept it.path
- the absolute path within the classpathclassLoader
- the class loader to load the resource with,
or null
for the thread context class loaderClassLoader.getResourceAsStream(String)
public ClassPathResource(java.lang.String path, java.lang.Class<?> clazz)
ClassPathResource
for Class
usage.
The path can be relative to the given class, or absolute within
the classpath via a leading slash.path
- relative or absolute path within the class pathclazz
- the class to load resources withClass.getResourceAsStream(java.lang.String)
protected ClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader, java.lang.Class<?> clazz)
ClassPathResource
with optional ClassLoader
and Class
. Only for internal usage.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 anypublic final java.lang.String getPath()
public final java.lang.ClassLoader getClassLoader()
public boolean exists()
exists
in interface Resource
exists
in class AbstractFileResolvingResource
ClassLoader.getResource(String)
,
Class.getResource(String)
protected java.net.URL resolveURL()
null
if not resolvablepublic java.io.InputStream getInputStream() throws java.io.IOException
null
)java.io.IOException
- if the stream could not be openedClassLoader.getResourceAsStream(String)
,
Class.getResourceAsStream(String)
public java.net.URL getURL() throws java.io.IOException
getURL
in interface Resource
getURL
in class AbstractResource
java.io.IOException
- if the resource cannot be resolved as URL,
i.e. if the resource is not available as descriptorClassLoader.getResource(String)
,
Class.getResource(String)
public Resource createRelative(java.lang.String relativePath)
createRelative
in interface Resource
createRelative
in class AbstractResource
relativePath
- the relative path (relative to this resource)StringUtils.applyRelativePath(String, String)
public java.lang.String getFilename()
getFilename
in interface Resource
getFilename
in class AbstractResource
StringUtils.getFilename(String)
public java.lang.String getDescription()
Object.toString()
public boolean equals(java.lang.Object obj)
equals
in class AbstractResource
Resource.getDescription()
public int hashCode()
hashCode
in class AbstractResource
Resource.getDescription()