public class DefaultResourceLoader extends java.lang.Object implements ResourceLoader
ResourceLoader
interface.
Used by ResourceEditor
, and serves as base class for
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.
FileSystemResourceLoader
,
ClassPathXmlApplicationContext
Modifier and Type | Class and Description |
---|---|
protected static class |
DefaultResourceLoader.ClassPathContextResource
ClassPathResource that explicitly expresses a context-relative path
through implementing the ContextResource interface.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.ClassLoader |
classLoader |
private java.util.Set<ProtocolResolver> |
protocolResolvers |
private java.util.Map<java.lang.Class<?>,java.util.Map<Resource,?>> |
resourceCaches |
CLASSPATH_URL_PREFIX
Constructor and Description |
---|
DefaultResourceLoader()
Create a new DefaultResourceLoader.
|
DefaultResourceLoader(java.lang.ClassLoader classLoader)
Create a new DefaultResourceLoader.
|
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. |
private java.lang.ClassLoader classLoader
private final java.util.Set<ProtocolResolver> protocolResolvers
private final java.util.Map<java.lang.Class<?>,java.util.Map<Resource,?>> resourceCaches
public DefaultResourceLoader()
ClassLoader access will happen using the thread context class loader at the time of this ResourceLoader's initialization.
Thread.getContextClassLoader()
public DefaultResourceLoader(java.lang.ClassLoader 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 accesspublic void setClassLoader(java.lang.ClassLoader classLoader)
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.
public java.lang.ClassLoader getClassLoader()
Will get passed to ClassPathResource's constructor for all ClassPathResource objects created by this resource loader.
getClassLoader
in interface ResourceLoader
null
if even the system
ClassLoader isn't accessible)ClassPathResource
public void addProtocolResolver(ProtocolResolver resolver)
Any such resolver will be invoked ahead of this loader's standard resolution rules. It may therefore also override any default rules.
getProtocolResolvers()
public java.util.Collection<ProtocolResolver> getProtocolResolvers()
public <T> java.util.Map<Resource,T> getResourceCache(java.lang.Class<T> valueType)
Resource
.valueType
- the value type, e.g. an ASM MetadataReader
Map
, shared at the ResourceLoader
levelpublic void clearResourceCaches()
getResourceCache(java.lang.Class<T>)
public Resource getResource(java.lang.String location)
ResourceLoader
The handle should always be a reusable resource descriptor,
allowing for multiple InputStreamSource.getInputStream()
calls.
Note that a Resource handle does not imply an existing resource;
you need to invoke Resource.exists()
to check for existence.
getResource
in interface ResourceLoader
location
- the resource locationnull
)ResourceLoader.CLASSPATH_URL_PREFIX
,
Resource.exists()
,
InputStreamSource.getInputStream()
protected Resource getResourceByPath(java.lang.String 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.
path
- the path to the resourceClassPathResource
,
FileSystemXmlApplicationContext.getResourceByPath(java.lang.String)
,
AbstractRefreshableWebApplicationContext.getResourceByPath(java.lang.String)