Spring中资源的加载原来是这么一回事啊! (6)

在 spring 中很多真真做操作的方法命名都是以 do 开头,我们从上面可以看到核心方法 #doFindPathMatchingFileResources(...) 、 #doFindPathMatchingJarResources(...) 这两个基本一样知识解析不懂的文件类型,另外还有一个方法 #determineRootDir(...) 方法实现了路径的解析,下面我们简单看一这两个实现。

3.5.4.1 determineRootDir(... )

determineRootDir 方法主要用于根路径的获取,解析路径中的通配符,代码如下:

/** * 通过给定的路径来获取根目录路径 * Determine the root directory for the given location. * <p>Used for determining the starting point for file matching, * resolving the root directory location to a {@code java.io.File} * and passing it into {@code retrieveMatchingFiles}, with the * remainder of the location as pattern. * <p>Will return "/WEB-INF/" for the pattern "/WEB-INF/*.xml", * for example. * @param location the location to check * @return the part of the location that denotes the root directory * @see #retrieveMatchingFiles */ protected String determineRootDir(String location) { //1. 找到最后 路径中出现的 : 的索引 +1 ,这里注意我们的路径时 类似 : classpath*: /web-inf/*.xml int prefixEnd = location.indexOf(':') + 1; //2. 获取跟路径长度 int rootDirEnd = location.length(); //3.判断冒号后面的路径是否包含通配符 如果包含,则截断最后一个由”/”分割的部分。 while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) { rootDirEnd = location.lastIndexOf('http://www.likecs.com/', rootDirEnd - 2) + 1; } // if (rootDirEnd == 0) { rootDirEnd = prefixEnd; } return location.substring(0, rootDirEnd); }

举例看一下:

原路径 获取跟路径
classpath*:/test/aa*/app-*.xml   classpath*:/test/  
classpath*:/test/aa/app-*.xml   classpath*:/test/aa  
3.5.4.2 doFindPathMatchingFileResources(... )

#doFindPathMatchingFileResources(...) 、 #doFindPathMatchingJarResources(...) 方法的的内部基本一致,只是解析不同的类型文件,我们这里只看其中一个则可,大家可以自行比对两者的区别。

我们跟一下 #doFindPathMatchingFileResources(...) 方法,方法内部调用较深,所以下面我主要把代码贴出来,注释已有,相信可以看的懂

#doFindPathMatchingFileResources(...) 代码:

/** * 查找文件系统符合给定的location的资源, 路径符合 ant 样式的通配符 * Find all resources in the file system that match the given location pattern * via the Ant-style PathMatcher. * @param rootDirResource the root directory as Resource * @param subPattern the sub pattern to match (below the root directory) * @return a mutable Set of matching Resource instances * @throws IOException in case of I/O errors * @see #retrieveMatchingFiles * @see org.springframework.util.PathMatcher */ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { File rootDir; try { //获取绝对路径对应的文件目录 rootDir = rootDirResource.getFile().getAbsoluteFile(); } catch (FileNotFoundException ex) { if (logger.isDebugEnabled()) { logger.debug("Cannot search for matching files underneath " + rootDirResource + " in the file system: " + ex.getMessage()); } return Collections.emptySet(); } catch (Exception ex) { if (logger.isInfoEnabled()) { logger.info("Failed to resolve " + rootDirResource + " in the file system: " + ex); } return Collections.emptySet(); } //调用真真处理方法 return doFindMatchingFileSystemResources(rootDir, subPattern); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdsgx.html