同步操作将从 huifer/Code-Analysis 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
类全路径: org.springframework.core.io.DefaultResourceLoader
首先来阅读成员变量内容.
public class DefaultResourceLoader implements ResourceLoader {
@Nullable
private ClassLoader classLoader;
/**
* 协议解析器列表
*/
private final Set<ProtocolResolver> protocolResolvers = new LinkedHashSet<>(4);
/**
* 资源缓存
*/
private final Map<Class<?>, Map<Resource, ?>> resourceCaches = new ConcurrentHashMap<>(4);
}
getResource
方法org.springframework.core.io.DefaultResourceLoader.getResource
Resource
@Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
// 获取协议解析器列表 循环
for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
Resource resource = protocolResolver.resolve(location, this);
if (resource != null) {
return resource;
}
}
// 路径地址是 / 开头
if (location.startsWith("/")) {
return getResourceByPath(location);
}
// 地址路径是 classpath: 开头
else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {
try {
// Try to parse the location as a URL...
// 尝试将 location 转换成 url 进行读取
URL url = new URL(location);
return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
}
catch (MalformedURLException ex) {
// No URL -> resolve as resource path.
return getResourceByPath(location);
}
}
}
在getResource
中提供了4种处理方式, 四种处理方式有四种处理方法.
协议解析器解析,对应接口
通过ProtocolResolver
接口方法进行解析后返回
处理 /
开头的路径
通过getResourceByPath
方法进行解析后返回
处理 classpath:
开头的路径
通过 new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader())
将对象创建后返回
尝试使用 URL 进行创建地址后解析
既然是尝试性的就有可能失败, 面对失败的情况 Spring 将器使用getResourceByPath
方法进行处理
如果尝试成功则包含两种处理方式, 两种处理方式的前提是是否为url文件协议(url文件协议是指file
、vfsfile
、vfs
三种协议).
FileUrlResource
对象创建UrlResource
对象创建方法签名: org.springframework.core.io.DefaultResourceLoader#getResourceByPath
方法作用: 创建ClassPathContextResource
对象.
protected Resource getResourceByPath(String path) {
return new ClassPathContextResource(path, getClassLoader());
}
/**
* ClassPathResource that explicitly expresses a context-relative path
* through implementing the ContextResource interface.
*/
protected static class ClassPathContextResource extends ClassPathResource implements ContextResource {
public ClassPathContextResource(String path, @Nullable ClassLoader classLoader) {
super(path, classLoader);
}
@Override
public String getPathWithinContext() {
return getPath();
}
@Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
return new ClassPathContextResource(pathToUse, getClassLoader());
}
}
getResource
法返回值涉及到Resource
接口. 详细可以阅读这篇文章,着重可以关注下面几个类
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。