Top Description Inners Fields Constructors Methods
org.springframework.core.io

public Class DefaultResourceLoader

extends Object
implements ResourceLoader
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.ResourceLoader
Known Direct Subclasses
org.springframework.core.io.FileSystemResourceLoader, org.springframework.core.io.ClassRelativeResourceLoader
Imports
java.net.MalformedURLException, .URL, java.util.Collection, .LinkedHashSet, .Map, .Set, java.util.concurrent.ConcurrentHashMap, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ResourceUtils, .StringUtils

Default implementation of the ResourceLoader interface.

Used by ResourceEditor, and serves as base class for org.springframework.context.support.AbstractApplicationContext. Can also be used standalone.

Will return a UrlResource if the location value is a URL, and a ClassPathResource if it is a non-URL path or a "classpath:" pseudo-URL.

Author
Juergen Hoeller
Since
10.03.2004
See Also
FileSystemResourceLoader, org.springframework.context.support.ClassPathXmlApplicationContext

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
DefaultResourceLoader.ClassPathContextResource

ClassPathResource that explicitly expresses a context-relative path through implementing the ContextResource interface.

Field Summary

Modifier and TypeField and Description
private ClassLoader
private final Set<ProtocolResolver>
private final Map<Class<?>, Map<Resource, ?>>

Constructor Summary

AccessConstructor and Description
public
DefaultResourceLoader()

Create a new DefaultResourceLoader.

public
DefaultResourceLoader(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
classLoader
)

Create a new DefaultResourceLoader.

Method Summary

Modifier and TypeMethod and Description
public void
addProtocolResolver(ProtocolResolver resolver)

Register the given resolver with this resource loader, allowing for additional protocols to be handled.

public void
clearResourceCaches()

Clear all resource caches in this resource loader.

public ClassLoader
getClassLoader()

Implements org.springframework.core.io.ResourceLoader.getClassLoader.

Return the ClassLoader to load class path resources with.
public Collection<ProtocolResolver>
getProtocolResolvers()

Return the collection of currently registered protocol resolvers, allowing for introspection as well as modification.

public Resource
getResource(String
the resource location
location
)

Implements org.springframework.core.io.ResourceLoader.getResource.

Return a Resource handle for the specified resource location.
protected Resource

Returns:

the corresponding Resource handle
getResourceByPath
(String
the path to the resource
path
)

Return a Resource handle for the resource at the given path.

public <T> Map<Resource, T>

Returns:

the cache Map, shared at the ResourceLoader level
getResourceCache
(Class<T>
the value type, e.g. an ASM MetadataReader
valueType
)

Obtain a cache for the given value type, keyed by Resource.

public void
setClassLoader(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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

classLoaderback to summary
private ClassLoader classLoader
Annotations
@Nullable
protocolResolversback to summary
private final Set<ProtocolResolver> protocolResolvers
resourceCachesback to summary
private final Map<Class<?>, Map<Resource, ?>> resourceCaches

Constructor Detail

DefaultResourceLoaderback to summary
public DefaultResourceLoader()

Create a new DefaultResourceLoader.

ClassLoader access will happen using the thread context class loader at the time of actual resource access (since 5.3). For more control, pass a specific ClassLoader to DefaultResourceLoader(ClassLoader).

See Also
java.lang.Thread#getContextClassLoader()
DefaultResourceLoaderback to summary
public DefaultResourceLoader(ClassLoader classLoader)

Create a new DefaultResourceLoader.

Parameters
classLoader: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

addProtocolResolverback to summary
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()
clearResourceCachesback to summary
public void clearResourceCaches()

Clear all resource caches in this resource loader.

Since
5.0
See Also
getResourceCache
getClassLoaderback to summary
public ClassLoader getClassLoader()

Implements org.springframework.core.io.ResourceLoader.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.

Returns:ClassLoader

Doc from org.springframework.core.io.ResourceLoader.getClassLoader.

the ClassLoader (only null if even the system ClassLoader isn't accessible)

Annotations
@Override
@Nullable
See Also
ClassPathResource
getProtocolResolversback to summary
public Collection<ProtocolResolver> getProtocolResolvers()

Return the collection of currently registered protocol resolvers, allowing for introspection as well as modification.

Since
4.3
See Also
addProtocolResolver(ProtocolResolver)
getResourceback to summary
public Resource getResource(String location)

Implements org.springframework.core.io.ResourceLoader.getResource.

Doc from org.springframework.core.io.ResourceLoader.getResource.

Return a Resource handle for the specified resource location.

The handle should always be a reusable resource descriptor, allowing for multiple Resource#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.

Parameters
location:String

the resource location

Returns:Resource

a corresponding Resource handle (never null)

Annotations
@Override
getResourceByPathback to summary
protected Resource getResourceByPath(String path)

Return a Resource handle for the resource at the given path.

The default implementation supports class path locations. This should be appropriate for standalone implementations but can be overridden, e.g. for implementations targeted at a Servlet container.

Parameters
path:String

the path to the resource

Returns:Resource

the corresponding Resource handle

See Also
ClassPathResource, org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath, org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
getResourceCacheback to summary
public <T> Map<Resource, T> getResourceCache(Class<T> valueType)

Obtain a cache for the given value type, keyed by Resource.

Parameters
valueType:Class<T>

the value type, e.g. an ASM MetadataReader

Returns:Map<Resource, T>

the cache Map, shared at the ResourceLoader level

Annotations
@SuppressWarnings:unchecked
Since
5.0
setClassLoaderback to summary
public void setClassLoader(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 actual resource access (since 5.3).

org.springframework.core.io back to summary

protected Class DefaultResourceLoader.ClassPathContextResource

extends ClassPathResource
implements ContextResource
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.ContextResource, org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource

ClassPathResource that explicitly expresses a context-relative path through implementing the ContextResource interface.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.ClassPathResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

Create a resource relative to this resource.
public String
getPathWithinContext()

Implements org.springframework.core.io.ContextResource.getPathWithinContext.

Return the path within the enclosing 'context'.
Inherited from org.springframework.core.io.ClassPathResource:
equalsexistsgetClassLoadergetDescriptiongetFilenamegetInputStreamgetPathgetURLhashCodeisReadableresolveURL

Constructor Detail

ClassPathContextResourceback to summary
public ClassPathContextResource(String path, ClassLoader classLoader)

Method Detail

createRelativeback to summary
public Resource createRelative(String relativePath)

Overrides org.springframework.core.io.ClassPathResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

Doc from org.springframework.core.io.Resource.createRelative.

Create a resource relative to this resource.

Parameters
relativePath:String

the relative path (relative to this resource)

Returns:Resource

the resource handle for the relative resource

Annotations
@Override
getPathWithinContextback to summary
public String getPathWithinContext()

Implements org.springframework.core.io.ContextResource.getPathWithinContext.

Doc from org.springframework.core.io.ContextResource.getPathWithinContext.

Return the path within the enclosing 'context'.

This is typically path relative to a context-specific root directory, e.g. a ServletContext root or a PortletContext root.

Annotations
@Override