org.springframework.core.io

Class DefaultResourceLoader

  • java.lang.Object
    • org.springframework.core.io.DefaultResourceLoader
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class and Description
      protected static class  DefaultResourceLoader.ClassPathContextResource
      ClassPathResource that explicitly expresses a context-relative path through implementing the ContextResource interface.
    • Constructor Summary

      Constructors 
      Constructor and Description
      DefaultResourceLoader()
      Create a new DefaultResourceLoader.
      DefaultResourceLoader(java.lang.ClassLoader classLoader)
      Create a new DefaultResourceLoader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void addProtocolResolver(ProtocolResolver resolver)
      Register the given resolver with this resource loader, allowing for additional protocols to be handled.
      void clearResourceCaches()
      Clear all resource caches in this resource loader.
      java.lang.ClassLoader getClassLoader()
      Return the ClassLoader to load class path resources with.
      java.util.Collection<ProtocolResolver> getProtocolResolvers()
      Return the collection of currently registered protocol resolvers, allowing for introspection as well as modification.
      Resource getResource(java.lang.String location)
      Return a Resource handle for the specified resource location.
      protected Resource getResourceByPath(java.lang.String path)
      Return a Resource handle for the resource at the given path.
      <T> java.util.Map<Resource,T> getResourceCache(java.lang.Class<T> valueType)
      Obtain a cache for the given value type, keyed by Resource.
      void setClassLoader(java.lang.ClassLoader classLoader)
      Specify the ClassLoader to load class path resources with, or null for using the thread context class loader at the time of actual resource access.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DefaultResourceLoader

        public DefaultResourceLoader()
        Create a new DefaultResourceLoader.

        ClassLoader access will happen using the thread context class loader at the time of this ResourceLoader's initialization.

        See Also:
        Thread.getContextClassLoader()
      • DefaultResourceLoader

        public DefaultResourceLoader(@Nullable
                                     java.lang.ClassLoader classLoader)
        Create a new DefaultResourceLoader.
        Parameters:
        classLoader - the ClassLoader to load class path resources with, or null for using the thread context class loader at the time of actual resource access
    • Method Detail

      • setClassLoader

        public void setClassLoader(@Nullable
                                   java.lang.ClassLoader classLoader)
        Specify the ClassLoader to load class path resources with, or null for using the thread context class loader at the time of actual resource access.

        The default is that ClassLoader access will happen using the thread context class loader at the time of this ResourceLoader's initialization.

      • getClassLoader

        @Nullable
        public java.lang.ClassLoader getClassLoader()
        Return the ClassLoader to load class path resources with.

        Will get passed to ClassPathResource's constructor for all ClassPathResource objects created by this resource loader.

        Specified by:
        getClassLoader in interface ResourceLoader
        Returns:
        the ClassLoader (only null if even the system ClassLoader isn't accessible)
        See Also:
        ClassPathResource
      • addProtocolResolver

        public void addProtocolResolver(ProtocolResolver resolver)
        Register the given resolver with this resource loader, allowing for additional protocols to be handled.

        Any such resolver will be invoked ahead of this loader's standard resolution rules. It may therefore also override any default rules.

        Since:
        4.3
        See Also:
        getProtocolResolvers()
      • getProtocolResolvers

        public java.util.Collection<ProtocolResolver> getProtocolResolvers()
        Return the collection of currently registered protocol resolvers, allowing for introspection as well as modification.
        Since:
        4.3
      • getResourceCache

        public <T> java.util.Map<Resource,T> getResourceCache(java.lang.Class<T> valueType)
        Obtain a cache for the given value type, keyed by Resource.
        Parameters:
        valueType - the value type, e.g. an ASM MetadataReader
        Returns:
        the cache Map, shared at the ResourceLoader level
        Since:
        5.0
      • getResource

        public Resource getResource(java.lang.String location)
        Description copied from interface: ResourceLoader
        Return a Resource handle for the specified resource location.

        The handle should always be a reusable resource descriptor, allowing for multiple InputStreamSource.getInputStream() calls.

        • Must support fully qualified URLs, e.g. "file:C:/test.dat".
        • Must support classpath pseudo-URLs, e.g. "classpath:test.dat".
        • Should support relative file paths, e.g. "WEB-INF/test.dat". (This will be implementation-specific, typically provided by an ApplicationContext implementation.)

        Note that a Resource handle does not imply an existing resource; you need to invoke Resource.exists() to check for existence.

        Specified by:
        getResource in interface ResourceLoader
        Parameters:
        location - the resource location
        Returns:
        a corresponding Resource handle (never null)
        See Also:
        ResourceLoader.CLASSPATH_URL_PREFIX, Resource.exists(), InputStreamSource.getInputStream()