类 ClassPathResource

  • 所有已实现的接口:
    InputStreamSource, Resource
    直接已知子类:
    DefaultResourceLoader.ClassPathContextResource

    public class ClassPathResource
    extends AbstractFileResolvingResource
    Copy from https://github.com/spring-projects/spring-framework.git, with less modifications 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)
    • 字段概要

      字段 
      修饰符和类型 字段 说明
      private java.lang.ClassLoader classLoader  
      private java.lang.Class<?> clazz  
      private java.lang.String path  
    • 方法概要

      所有方法 实例方法 具体方法 
      修饰符和类型 方法 说明
      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 other)
      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.
      boolean isReadable()
      This implementation checks for the resolution of a resource URL upfront, then proceeding with AbstractFileResolvingResource's length check.
      protected java.net.URL resolveUrl()
      Resolves a URL for the underlying class path resource.
      • 从类继承的方法 java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 字段详细资料

      • path

        private final java.lang.String path
      • classLoader

        private java.lang.ClassLoader classLoader
      • clazz

        private java.lang.Class<?> clazz
    • 构造器详细资料

      • ClassPathResource

        public ClassPathResource​(java.lang.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​(java.lang.String path,
                                 java.lang.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​(java.lang.String path,
                                 java.lang.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​(java.lang.String path,
                                    java.lang.ClassLoader classLoader,
                                    java.lang.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 java.lang.String getPath()
        Return the path for this resource (as resource path within the class path).
      • getClassLoader

        public final java.lang.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

        protected java.net.URL resolveUrl()
        Resolves a URL for the underlying class path resource.
        返回:
        the resolved URL, or null if not resolvable
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        This implementation opens an InputStream for the given class path resource.
        返回:
        the input stream for the underlying resource (must not be null)
        抛出:
        java.io.FileNotFoundException - if the underlying resource does not exist
        java.io.IOException - if the content stream could not be opened
        另请参阅:
        ClassLoader.getResourceAsStream(String), Class.getResourceAsStream(String)
      • getUrl

        public java.net.URL getUrl()
                            throws java.io.IOException
        This implementation returns a URL for the underlying class path resource, if available.
        指定者:
        getUrl 在接口中 Resource
        覆盖:
        getUrl 在类中 AbstractResource
        抛出:
        java.io.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)
      • getDescription

        public java.lang.String getDescription()
        This implementation returns a description that includes the class path location.
        另请参阅:
        Object.toString()