public class DefaultResourceLoader extends Object implements ResourceLoader
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.
FileSystemResourceLoader
,
org.springframework.context.support.ClassPathXmlApplicationContext
CLASSPATH_URL_PREFIX
构造器和说明 |
---|
DefaultResourceLoader()
Create a new DefaultResourceLoader.
|
DefaultResourceLoader(ClassLoader classLoader)
Create a new DefaultResourceLoader.
|
限定符和类型 | 方法和说明 |
---|---|
ClassLoader |
getClassLoader()
Return the ClassLoader to load class path resources with.
|
Resource |
getResource(String location)
DefaultResourceLoader取resource的顺序:
判断是否为 classpath URL,例如 classpath:test.dat
判断是否为 标准URL 路径,例如 file:C:/test.dat,http://www.math.com/test.html
交给子类的 getResourceByPath(location) 方法进一步去判断。
|
protected Resource |
getResourceByPath(String path)
Return a Resource handle for the resource at the given path.
|
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. |
public DefaultResourceLoader()
ClassLoader access will happen using the thread context class loader at the time of this ResourceLoader's initialization.
public DefaultResourceLoader(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(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 ClassLoader getClassLoader()
Will get passed to ClassPathResource's constructor for all ClassPathResource objects created by this resource loader.
getClassLoader
在接口中 ResourceLoader
null
if even the system
ClassLoader isn't accessible)ClassPathResource
public Resource getResource(String location)
DefaultResourceLoader取resource的顺序: 判断是否为 classpath URL,例如 classpath:test.dat 判断是否为 标准URL 路径,例如 file:C:/test.dat,http://www.math.com/test.html 交给子类的 getResourceByPath(location) 方法进一步去判断。 例如, 对于FileSystemResourceLoader子类,则按照FileSystemResource去解析 对于ServletContextResourceLoader子类,则按照ServletContextResource去解析 注意:如果文件不存在,该方法不会报错,而是会返回一个不可用的Resource实例, 对于 DefaultResourceLoader,返回的是DefaultResourceLoader.ClassPathContextResource
对于 FileSystemResourceLoader,返回的是FileSystemResourceLoader.FileSystemContextResource
对于 ServletContextResourceLoader,返回的是org.zollty.util.resource.web.ServletContextResource
getResource
在接口中 ResourceLoader
location
- the resource locationResourceLoader.CLASSPATH_URL_PREFIX
,
org.springframework.core.io.Resource#exists
,
org.springframework.core.io.Resource#getInputStream
protected Resource getResourceByPath(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
,
org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
,
org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
Copyright © 2018 jretty-org. All rights reserved.