Package org.springframework.core.io

# Class DefaultResourceLoader

java.lang.Object
org.springframework.core.io.DefaultResourceLoader

All Implemented Interfaces:
  `ResourceLoader`

Direct Known Subclasses:
  `ClassRelativeResourceLoader`, `FileSystemResourceLoader`

public class DefaultResourceLoader extends Object implements ResourceLoader
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.

Since:
  10.03.2004
Author:
  Juergen Hoeller
See Also:
  - `FileSystemResourceLoader`
  - `org.springframework.context.support.ClassPathXmlApplicationContext`

## Nested Class Summary

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

## Field Summary

### Fields inherited from interface org.springframework.core.io.ResourceLoader

`CLASSPATH_URL_PREFIX`

## Constructor Summary

Constructors
Constructor
Description
`DefaultResourceLoader()`
Create a new DefaultResourceLoader.
`DefaultResourceLoader(ClassLoader classLoader)`
Create a new DefaultResourceLoader.

## Method Summary

Modifier and Type
Method
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.
`ClassLoader`
`getClassLoader()`
Return the ClassLoader to load class path resources with.
`Collection<ProtocolResolver>`
`getProtocolResolvers()`
Return the collection of currently registered protocol resolvers, allowing for introspection as well as modification.
`Resource`
`getResource(String location)`
Return a `Resource` handle for the specified resource location.
`protected Resource`
`getResourceByPath(String path)`
Return a Resource handle for the resource at the given path.
`<T> Map<Resource,T>`
`getResourceCache(Class<T> valueType)`
Obtain a cache for the given value type, keyed by `Resource`.
`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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DefaultResourceLoader

public DefaultResourceLoader()
Create a new DefaultResourceLoader.

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

See Also:
  - `Thread.getContextClassLoader()`

### DefaultResourceLoader

public DefaultResourceLoader(@Nullable 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 Details

### setClassLoader

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

### getClassLoader

@Nullable public 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 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)`

### getResourceCache

public <T> Map<Resource,T> getResourceCache(Class<T> valueType)
Obtain a cache for the given value type, keyed by `Resource`.

Parameters:
  `valueType` - the value type, for example, an ASM `MetadataReader`
Returns:
  the cache `Map`, shared at the `ResourceLoader` level
Since:
  5.0

### clearResourceCaches

public void clearResourceCaches()
Clear all resource caches in this resource loader.

Since:
  5.0
See Also:
  - `getResourceCache(java.lang.Class<T>)`

### getResource

public Resource getResource(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, for example, "file:C:/test.dat".
- Must support classpath pseudo-URLs, for example, "classpath:test.dat".
- Should support relative file paths, for example, "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()`

### getResourceByPath

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, for example, for implementations targeted at a Servlet container.

Parameters:
  `path` - the path to the resource
Returns:
  the corresponding Resource handle
See Also:
  - `ClassPathResource`
  - `org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath`
  - `org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath`