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

#findAllClassPathResources(...) 方法代码:

/** * 通过ClassLoader 来 加载所有的 class location * Find all class location resources with the given location via the ClassLoader. * Delegates to {@link #doFindAllClassPathResources(String)}. * @param location the absolute path within the classpath * @return the result as Resource array * @throws IOException in case of I/O errors * @see java.lang.ClassLoader#getResources * @see #convertClassLoaderURL */ protected Resource[] findAllClassPathResources(String location) throws IOException { String path = location; if (path.startsWith("http://www.likecs.com/")) { path = path.substring(1); } //真实处理方法, 得到资源结果集 Set<Resource> result = doFindAllClassPathResources(path); if (logger.isTraceEnabled()) { logger.trace("Resolved classpath location [" + location + "] to resources " + result); } return result.toArray(new Resource[0]); }

#doFindAllClassPathResources(...) 方法代码:

/** * Find all class location resources with the given path via the ClassLoader. * Called by {@link #findAllClassPathResources(String)}. * @param path the absolute path within the classpath (never a leading slash) * @return a mutable Set of matching Resource instances * @since 4.1.1 */ protected Set<Resource> doFindAllClassPathResources(String path) throws IOException { Set<Resource> result = new LinkedHashSet<>(16); ClassLoader cl = getClassLoader(); //1.通过ClassLoader获取所有的URl Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path)); while (resourceUrls.hasMoreElements()) { //将URl转换为 UrlResource URL url = resourceUrls.nextElement(); result.add(convertClassLoaderURL(url)); } if ("".equals(path)) { // The above result is likely to be incomplete, i.e. only containing file system references. // We need to have pointers to each of the jar files on the classpath as well... //添加所有的jar包 addAllClassLoaderJarRoots(cl, result); } return result; }

方法内相对简单,主要是通过ClassLoader来加载目录下的jar资源,详细不再贴出来,可以自行查看

本文由AnonyStar 发布,可转载但需声明原文出处。
仰慕「优雅编码的艺术」 坚信熟能生巧,努力改变人生
欢迎关注微信公账号 :coder简码 获取更多优质文章
更多文章关注笔者博客 :IT简码

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

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