SimpleUrlHandlerMapping静态资源处理器二
2023-07-27 本文已影响0人
程序员札记
获取资源
@Nullable
protected Resource getResource(HttpServletRequest request) throws IOException {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);//获取前面放入的路径属性
...
path = processPath(path);//处理路径,获取资源名字
...
Resource resource = this.resolverChain.resolveResource(request, path, getLocations());//解析资源
if (resource != null) {//资源转换
resource = this.transformerChain.transform(request, resource);
}
return resource;
}
解析资源
最后到PathResourceResolver
的getResource
。
@Nullable
private Resource getResource(String resourcePath, @Nullable HttpServletRequest request,
List<? extends Resource> locations) {
//变量查看是否有该资源
for (Resource location : locations) {
try {
String pathToUse = encodeIfNecessary(resourcePath, request, location);//可能要编码
Resource resource = getResource(pathToUse, location);//获得资源,用URL读取
if (resource != null) {
return resource;
}
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
String error = "Skip location [" + location + "] due to error";
if (logger.isTraceEnabled()) {
logger.trace(error, ex);
}
else {
logger.debug(error + ": " + ex.getMessage());
}
}
}
}
return null;
}
会挨个遍历以下资源路径下是否有该资源。
![](https://img.haomeiwen.com/i26273155/c6609e6759f39324.png)
资源是否可读
![](https://img.haomeiwen.com/i26273155/0c0945ee4e281672.png)
封装路径到该资源路径下,并创建ClassPathResource
:
![](https://img.haomeiwen.com/i26273155/24be9fb97ae1a6e3.png)
AbstractFileResolvingResource的isReadable
先获取URL
判断是文件路径还是网络的,然后分别尝试去查找:
![](https://img.haomeiwen.com/i26273155/e468e9eb70187cc7.png)
其实就是用类加载器去加载路径,返回URL
。
![](https://img.haomeiwen.com/i26273155/6f2600f4ba7f2b6c.png)
![](https://img.haomeiwen.com/i26273155/a6131fe9adf7da21.png)
根据协议来判断是不是文件:
![](https://img.haomeiwen.com/i26273155/c222a432c8645041.png)
最后创建一个文件对象,看是否可读且不是目录。
如果是网络的话就直接连接,然后获取内容长度来判断存不存在。
写出资源
资源获取到了,要写出去呀,就是调用AbstractHttpMessageConverter
的write
方法,添加好内容头信息,然后写入输出缓存,并刷出去。
![](https://img.haomeiwen.com/i26273155/d8b6a8fd153a6ee6.png)
内部就是读资源,然后写入输出流中:
![](https://img.haomeiwen.com/i26273155/6a8f5ea286567682.png)
其实他在拷贝数据的时候已经刷出去了:
![](https://img.haomeiwen.com/i26273155/f18093745499c780.png)
这样,静态资源的处理流程基本知道了,主要SimpleUrlHandlerMapping
是对一些资源路径做了映射处理,有处理器处理,所以可以直接就访问资源:
classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,/