Class ClassPathResource

  • All Implemented Interfaces:
    InputStreamResource, Resource

    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.

    See Also:
    ClassLoader.getResourceAsStream(String), Code copied from Spring source. Modifications made (mostly removal of methods) by Per Wendel.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.ClassLoader classLoader  
      private java.lang.Class<?> clazz  
      private java.lang.String path  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ClassPathResource​(java.lang.String path)
      Create a new ClassPathResource for ClassLoader usage.
        ClassPathResource​(java.lang.String path, java.lang.ClassLoader classLoader)
      Create a new ClassPathResource for ClassLoader usage.
      protected ClassPathResource​(java.lang.String path, java.lang.ClassLoader classLoader, java.lang.Class<?> clazz)
      Create a new ClassPathResource with optional ClassLoader and Class.
    • Field Detail

      • path

        private final java.lang.String path
      • classLoader

        private java.lang.ClassLoader classLoader
      • clazz

        private java.lang.Class<?> clazz
    • 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,
                                 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

        protected ClassPathResource​(java.lang.String path,
                                    java.lang.ClassLoader classLoader,
                                    java.lang.Class<?> clazz)
        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

      • isValid

        private static boolean isValid​(java.lang.String path)
      • isInvalidPath

        private static boolean isInvalidPath​(java.lang.String path)
      • getPath

        public final java.lang.String getPath()
        Return the path for this resource (as resource path within the class path).
        Returns:
        the path
      • 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
        Returns:
        if exists.
        See Also:
        ClassLoader.getResource(String), Class.getResource(String)
      • 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.
        Throws:
        java.io.IOException - if the 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.
        Specified by:
        getURL in interface Resource
        Overrides:
        getURL in class AbstractResource
        Returns:
        the url.
        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.
        Returns:
        the description.
        See Also:
        Object.toString()