org.springframework.core.io

## 类 ClassPathResource

- java.lang.Object
- - org.springframework.core.io.AbstractResource
  - - org.springframework.core.io.AbstractFileResolvingResource
    - - org.springframework.core.io.ClassPathResource

- 所有已实现的接口:
    InputStreamSource, Resource
  直接已知子类:
    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 URL.

  从以下版本开始:
    28.12.2003
  作者:
    Juergen Hoeller, Sam Brannen
  另请参阅:
    `ClassLoader.getResourceAsStream(String)`, `Class.getResourceAsStream(String)`

- - ### 构造器概要

    构造器
    限定符 | 构造器和说明
    --- | ---
    `` | `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.
    `protected` | `ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz)` 已过时。 as of 4.3.13, in favor of selective use of `ClassPathResource(String, ClassLoader)` vs `ClassPathResource(String, Class)`

  - ### 方法概要

    所有方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `Resource` | `createRelative(String relativePath)` This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.
    `boolean` | `equals(Object other)` This implementation compares the underlying class path locations.
    `boolean` | `exists()` This implementation checks for the resolution of a resource URL.
    `ClassLoader` | `getClassLoader()` Return the ClassLoader that this resource will be obtained from.
    `String` | `getDescription()` This implementation returns a description that includes the 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 given class path resource.
    `String` | `getPath()` Return the path for this resource (as 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.
    `protected URL` | `resolveURL()` Resolves a URL for the underlying class path resource.

    - ### 从类继承的方法 org.springframework.core.io.AbstractFileResolvingResource

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

    - ### 从类继承的方法 org.springframework.core.io.AbstractResource

      `getURI, isOpen, toString`

    - ### 从类继承的方法 java.lang.Object

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

- - ### 构造器详细资料

    - #### 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 thread context class loader will be used for loading the resource.

      参数:
        `path` - the absolute path within the class path
      另请参阅:
        `ClassLoader.getResourceAsStream(String)`, `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.
      参数:
        `path` - the absolute path within the classpath
        `classLoader` - the class loader to load the resource with, or `null` for the thread context class loader
      另请参阅:
        `ClassLoader.getResourceAsStream(String)`

    - #### 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 classpath via a leading slash.
      参数:
        `path` - relative or absolute path within the class path
        `clazz` - the class to load resources with
      另请参阅:
        `Class.getResourceAsStream(java.lang.String)`

    - #### ClassPathResource

          @Deprecated
          protected ClassPathResource(String path,
                                                   @Nullable
                                                   ClassLoader classLoader,
                                                   @Nullable
                                                   Class<?> clazz)

      已过时。 as of 4.3.13, in favor of selective use of `ClassPathResource(String, ClassLoader)` vs `ClassPathResource(String, Class)`
      Create a new `ClassPathResource` with optional `ClassLoader` and `Class`. Only for internal usage.
      参数:
        `path` - relative or absolute path within the classpath
        `classLoader` - the class loader to load the resource with, if any
        `clazz` - the class to load resources with, if any

  - ### 方法详细资料

    - #### getPath

          public final String getPath()

      Return the path for this resource (as resource path within the class path).

    - #### 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.
      指定者:
        `exists` 在接口中 `Resource`
      覆盖:
        `exists` 在类中 `AbstractFileResolvingResource`
      另请参阅:
        `ClassLoader.getResource(String)`, `Class.getResource(String)`

    - #### resolveURL

          @Nullable
          protected URL resolveURL()

      Resolves a URL for the underlying class path resource.
      返回:
        the resolved URL, or `null` if not resolvable

    - #### getInputStream

          public InputStream getInputStream()
                                     throws IOException

      This implementation opens an InputStream for the given class path resource.
      返回:
        the input stream for the underlying resource (must not be `null`)
      抛出:
        `FileNotFoundException` - if the underlying resource doesn't exist
        `IOException` - if the content stream could not be opened
      另请参阅:
        `ClassLoader.getResourceAsStream(String)`, `Class.getResourceAsStream(String)`

    - #### getURL

          public URL getURL()
                     throws IOException

      This implementation returns a URL for the underlying class path resource, if available.
      指定者:
        `getURL` 在接口中 `Resource`
      覆盖:
        `getURL` 在类中 `AbstractResource`
      抛出:
        `IOException` - if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
      另请参阅:
        `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 of the underlying resource of this descriptor.
      指定者:
        `createRelative` 在接口中 `Resource`
      覆盖:
        `createRelative` 在类中 `AbstractResource`
      参数:
        `relativePath` - the relative path (relative to this resource)
      返回:
        the resource handle for the relative resource
      另请参阅:
        `StringUtils.applyRelativePath(String, String)`

    - #### getFilename

          @Nullable
          public String getFilename()

      This implementation returns the name of the file that this class path resource refers to.
      指定者:
        `getFilename` 在接口中 `Resource`
      覆盖:
        `getFilename` 在类中 `AbstractResource`
      另请参阅:
        `StringUtils.getFilename(String)`

    - #### getDescription

          public String getDescription()

      This implementation returns a description that includes the class path location.
      另请参阅:
        `Object.toString()`

    - #### equals

          public boolean equals(Object other)

      This implementation compares the underlying class path locations.
      覆盖:
        `equals` 在类中 `AbstractResource`
      另请参阅:
        `Resource.getDescription()`

    - #### hashCode

          public int hashCode()

      This implementation returns the hash code of the underlying class path location.
      覆盖:
        `hashCode` 在类中 `AbstractResource`
      另请参阅:
        `Resource.getDescription()`