org.springframework.core.io

Class 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 URL.

    Since:
    28.12.2003
    Author:
    Juergen Hoeller, Sam Brannen
    See Also:
    ClassLoader.getResourceAsStream(String), Class.getResourceAsStream(String)
    • Constructor Detail

      • 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.

        Parameters:
        path - the absolute path within the class path
        See Also:
        ClassLoader.getResourceAsStream(String), ClassUtils.getDefaultClassLoader()
      • ClassPathResource

        public ClassPathResource(java.lang.String path,
                                 @Nullable
                                 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.
        Parameters:
        path - the absolute path within the classpath
        classLoader - the class loader to load the resource with, or null for the thread context class loader
        See Also:
        ClassLoader.getResourceAsStream(String)
      • ClassPathResource

        public ClassPathResource(java.lang.String path,
                                 @Nullable
                                 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.
        Parameters:
        path - relative or absolute path within the class path
        clazz - the class to load resources with
        See Also:
        Class.getResourceAsStream(java.lang.String)
      • ClassPathResource

        @Deprecated
        protected ClassPathResource(java.lang.String path,
                                                @Nullable
                                                java.lang.ClassLoader classLoader,
                                                @Nullable
                                                java.lang.Class<?> clazz)
        Deprecated. 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.
        Parameters:
        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
    • Method Detail

      • getPath

        public final java.lang.String getPath()
        Return the path for this resource (as resource path within the class path).
      • getClassLoader

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

        @Nullable
        protected java.net.URL resolveURL()
        Resolves a URL for the underlying class path resource.
        Returns:
        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.
        Returns:
        the input stream for the underlying resource (must not be null)
        Throws:
        java.io.FileNotFoundException - if the underlying resource doesn't exist
        java.io.IOException - if the content stream could not be opened
        See Also:
        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.
        Specified by:
        getURL in interface Resource
        Overrides:
        getURL in class AbstractResource
        Throws:
        java.io.IOException - if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
        See Also:
        ClassLoader.getResource(String), Class.getResource(String)
      • getDescription

        public java.lang.String getDescription()
        This implementation returns a description that includes the class path location.
        See Also:
        Object.toString()