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
`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:
  - `ClassLoader.getResourceAsStream(String)`
  - `ClassLoader.getResource(String)`
  - `Class.getResourceAsStream(String)`
  - `Class.getResource(String)`

## Constructor Summary

Constructors
Constructor
Description
`ClassPathResource(String path)`
Create a new `ClassPathResource` for `ClassLoader` usage.
`ClassPathResource(String path, Class<?> clazz)`
Create a new `ClassPathResource` for `Class` usage.
`ClassPathResource(String path, ClassLoader classLoader)`
Create a new `ClassPathResource` for `ClassLoader` usage.

## Method Summary

Modifier and Type
Method
Description
`Resource`
`createRelative(String relativePath)`
This implementation creates a `ClassPathResource`, applying the given path relative to the path used to create this descriptor.
`boolean`
`equals(Object other)`
This implementation compares the underlying class path locations and associated class loaders.
`boolean`
`exists()`
This implementation checks for the resolution of a resource URL.
`final ClassLoader`
`getClassLoader()`
Return the `ClassLoader` that this resource will be obtained from.
`String`
`getDescription()`
This implementation returns a description that includes the absolute class path location.
`String`
`getFilename()`
This implementation returns the name of the file that this class path resource refers to.
`InputStream`
`getInputStream()`
This implementation opens an `InputStream` for the underlying class path resource, if available.
`final String`
`getPath()`
Return the absolute path for this resource, as a cleaned resource path within the class path.
`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.
`boolean`
`isReadable()`
This implementation checks for the resolution of a resource URL upfront, then proceeding with `AbstractFileResolvingResource`'s length check.
`protected URL`
`resolveURL()`
Resolves a `URL` for the underlying class path resource.

### Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource

`contentLength, customizeConnection, customizeConnection, getFile, getFile, getFileForLastModifiedCheck, isFile, isFile, lastModified, readableChannel`

### Methods inherited from class org.springframework.core.io.AbstractResource

`getURI, isOpen, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.springframework.core.io.Resource

`getContentAsByteArray, getContentAsString`

## Constructor Details

### ClassPathResource

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` - the absolute path within the class path
See Also:
  - `ClassUtils.getDefaultClassLoader()`

### ClassPathResource

public ClassPathResource(String path, @Nullable 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` - the absolute path within the class path
  `classLoader` - the class loader to load the resource with
See Also:
  - `ClassUtils.getDefaultClassLoader()`

### ClassPathResource

public ClassPathResource(String path, @Nullable 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` - relative or absolute path within the class path
  `clazz` - the class to load resources with
See Also:
  - `ClassUtils.getDefaultClassLoader()`
  - `ModuleResource`

## Method Details

### getPath

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)`.

### getClassLoader

@Nullable 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:
  `exists` in interface `Resource`
Overrides:
  `exists` in class `AbstractFileResolvingResource`
See Also:
  - `ClassLoader.getResource(String)`
  - `Class.getResource(String)`

### isReadable

public boolean isReadable()
This implementation checks for the resolution of a resource URL upfront, then proceeding with `AbstractFileResolvingResource`'s length check.

Specified by:
  `isReadable` in interface `Resource`
Overrides:
  `isReadable` in class `AbstractFileResolvingResource`
See Also:
  - `ClassLoader.getResource(String)`
  - `Class.getResource(String)`

### resolveURL

@Nullable protected URL resolveURL()
Resolves a `URL` for the underlying class path resource.

Returns:
  the resolved URL, or `null` if not resolvable

### getInputStream

public InputStream getInputStream() throws IOException
This implementation opens an `InputStream` for the underlying class path resource, if available.

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:
  - `ClassLoader.getResourceAsStream(String)`
  - `Class.getResourceAsStream(String)`
  - `ClassLoader.getSystemResourceAsStream(String)`

### getURL

public URL getURL() throws IOException
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:
  - `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 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:
  - `StringUtils.applyRelativePath(String, String)`

### getFilename

@Nullable public String 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:
  - `StringUtils.getFilename(String)`

### getDescription

public String getDescription()
This implementation returns a description that includes the absolute class path location.

See Also:
  - `Object.toString()`

### equals

public boolean equals(@Nullable Object other)
This implementation compares the underlying class path locations and associated class loaders.

Overrides:
  `equals` in class `AbstractResource`
See Also:
  - `getPath()`
  - `getClassLoader()`

### hashCode

public int hashCode()
This implementation returns the hash code of the underlying class path location.

Overrides:
  `hashCode` in class `AbstractResource`
See Also:
  - `getPath()`