Top Description Fields Constructors Methods
org.springframework.core.io

public Class ClassPathResource

extends AbstractFileResolvingResource
Class Inheritance
Known Direct Subclasses
org.springframework.core.io.ClassRelativeResourceLoader.ClassRelativeContextResource, org.springframework.core.io.DefaultResourceLoader.ClassPathContextResource
Imports
java.io.FileNotFoundException, .IOException, .InputStream, java.net.URL, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ObjectUtils, .StringUtils

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.

Authors
Juergen Hoeller, Sam Brannen
Since
28.12.2003
See Also
ClassLoader#getResourceAsStream(String), ClassLoader#getResource(String), Class#getResourceAsStream(String), Class#getResource(String)

Field Summary

Modifier and TypeField and Description
private final String
private final ClassLoader
private final Class<?>
private final String
path

Internal representation of the original path supplied by the user, used for creating relative paths and resolving URLs and InputStreams.

Constructor Summary

AccessConstructor and Description
public
ClassPathResource(String
the absolute path within the class path
path
)

Create a new ClassPathResource for ClassLoader usage.

public
ClassPathResource(String
the absolute path within the class path
path
,
ClassLoader
the class loader to load the resource with
classLoader
)

Create a new ClassPathResource for ClassLoader usage.

public
ClassPathResource(String
relative or absolute path within the class path
path
,
Class<?>
the class to load resources with
clazz
)

Create a new ClassPathResource for Class usage.

Method Summary

Modifier and TypeMethod and Description
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a ClassPathResource, applying the given path relative to the path used to create this descriptor.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying class path locations and associated class loaders.

public boolean
exists()

Overrides org.springframework.core.io.AbstractFileResolvingResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation checks for the resolution of a resource URL.

public final ClassLoader
getClassLoader()

Return the ClassLoader that this resource will be obtained from.

public String
getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the absolute class path location.

public String
getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the name of the file that this class path resource refers to.

public InputStream
getInputStream()

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation opens an InputStream for the underlying class path resource, if available.

public final String
getPath()

Return the absolute path for this resource, as a cleaned resource path within the class path.

public URL
getURL()

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation returns a URL for the underlying class path resource, if available.

public int
hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying class path location.

public boolean
isReadable()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation checks for the resolution of a resource URL upfront, then proceeding with AbstractFileResolvingResource's length check.

protected URL

Returns:

the resolved URL, or null if not resolvable
resolveURL
()

Resolves a URL for the underlying class path resource.

Inherited from org.springframework.core.io.AbstractFileResolvingResource:
checkReadablecontentLengthcustomizeConnectioncustomizeConnectiongetFilegetFilegetFileForLastModifiedCheckisFileisFilelastModifiedreadableChannel

Field Detail

absolutePathback to summary
private final String absolutePath
classLoaderback to summary
private final ClassLoader classLoader
Annotations
@Nullable
clazzback to summary
private final Class<?> clazz
Annotations
@Nullable
pathback to summary
private final String path

Internal representation of the original path supplied by the user, used for creating relative paths and resolving URLs and InputStreams.

Constructor Detail

ClassPathResourceback to summary
public ClassPathResource(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 default class loader will be used for loading the resource.

Parameters
path:String

the absolute path within the class path

See Also
ClassUtils#getDefaultClassLoader()
ClassPathResourceback to summary
public ClassPathResource(String path, 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.

If the supplied ClassLoader is null, the default class loader will be used for loading the resource.

Parameters
path:String

the absolute path within the class path

classLoader:ClassLoader

the class loader to load the resource with

See Also
ClassUtils#getDefaultClassLoader()
ClassPathResourceback to summary
public ClassPathResource(String path, Class<?> clazz)

Create a new ClassPathResource for Class usage.

The path can be relative to the given class, or absolute within the class path via a leading slash.

If the supplied Class is null, 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. See ModuleResource and its javadoc.

Parameters
path:String

relative or absolute path within the class path

clazz:Class<?>

the class to load resources with

See Also
ClassUtils#getDefaultClassLoader(), ModuleResource

Method Detail

createRelativeback to summary
public Resource createRelative(String relativePath)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a ClassPathResource, applying the given path relative to the path used to create this descriptor.

Parameters
relativePath:String

Doc from org.springframework.core.io.Resource.createRelative.

the relative path (relative to this resource)

Returns:Resource

Doc from org.springframework.core.io.Resource.createRelative.

the resource handle for the relative resource

Annotations
@Override
See Also
StringUtils#applyRelativePath(String, String)
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying class path locations and associated class loaders.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
See Also
getPath(), getClassLoader()
existsback to summary
public boolean exists()

Overrides org.springframework.core.io.AbstractFileResolvingResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation checks for the resolution of a resource URL.

Annotations
@Override
See Also
ClassLoader#getResource(String), Class#getResource(String)
getClassLoaderback to summary
public final ClassLoader getClassLoader()

Return the ClassLoader that this resource will be obtained from.

Annotations
@Nullable
getDescriptionback to summary
public String getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the absolute class path location.

Annotations
@Override
getFilenameback to summary
public String getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the name of the file that this class path resource refers to.

Annotations
@Override
@Nullable
See Also
StringUtils#getFilename(String)
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation opens an InputStream for the underlying class path resource, if available.

Returns:InputStream

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

the input stream for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

if the content stream could not be opened

See Also
ClassLoader#getResourceAsStream(String), Class#getResourceAsStream(String), ClassLoader#getSystemResourceAsStream(String)
getPathback to summary
public final String 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).

getURLback to summary
public URL getURL() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation returns a URL for the underlying class path resource, if available.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURL.

if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor

See Also
ClassLoader#getResource(String), Class#getResource(String)
hashCodeback to summary
public int hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying class path location.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object

Annotations
@Override
See Also
getPath()
isReadableback to summary
public boolean isReadable()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation checks for the resolution of a resource URL upfront, then proceeding with AbstractFileResolvingResource's length check.

Annotations
@Override
See Also
ClassLoader#getResource(String), Class#getResource(String)
resolveURLback to summary
protected URL resolveURL()

Resolves a URL for the underlying class path resource.

Returns:URL

the resolved URL, or null if not resolvable

Annotations
@Nullable