1. /*
  2. * 用一个给定的类加载器或者给定的类来加载资源
  3. */
  4. public class ClassPathResource extends AbstractFileResolvingResource {
  5.  
  6. private final String path;
  7.  
  8. private ClassLoader classLoader;
  9.  
  10. private Class<?> clazz;
  11.  
  12. /**
  13. * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
  14. * A leading slash will be removed, as the ClassLoader resource access
  15. * methods will not accept it.
  16. * <p>The thread context class loader will be used for
  17. * loading the resource.
  18. * @param path the absolute path within the class path
  19. * @see java.lang.ClassLoader#getResourceAsStream(String)
  20. * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
  21. */
  22. //类路径的决定路径
  23. public ClassPathResource(String path) {
  24. this(path, (ClassLoader) null);
  25. }
  26.  
  27. /**
  28. * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
  29. * A leading slash will be removed, as the ClassLoader resource access
  30. * methods will not accept it.
  31. * @param path the absolute path within the classpath
  32. * @param classLoader the class loader to load the resource with,
  33. * or {@code null} for the thread context class loader
  34. * @see ClassLoader#getResourceAsStream(String)
  35. */
  36. //创建一个类路径资源给类加载器使用
  37. public ClassPathResource(String path, ClassLoader classLoader) {
  38. Assert.notNull(path, "Path must not be null");
  39. String pathToUse = StringUtils.cleanPath(path);
  40. if (pathToUse.startsWith("/")) {
  41. pathToUse = pathToUse.substring(1);
  42. }
  43. this.path = pathToUse;
  44. this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());//获得默认的类加载器
  45. }
  46.  
  47. /**
  48. * Create a new {@code ClassPathResource} for {@code Class} usage.
  49. * The path can be relative to the given class, or absolute within
  50. * the classpath via a leading slash.
  51. * @param path relative or absolute path within the class path
  52. * @param clazz the class to load resources with
  53. * @see java.lang.Class#getResourceAsStream
  54. */
  55. public ClassPathResource(String path, Class<?> clazz) {
  56. Assert.notNull(path, "Path must not be null");
  57. this.path = StringUtils.cleanPath(path);
  58. this.clazz = clazz;
  59. }
  60.  
  61. /**
  62. * Create a new {@code ClassPathResource} with optional {@code ClassLoader}
  63. * and {@code Class}. Only for internal usage.
  64. * @param path relative or absolute path within the classpath
  65. * @param classLoader the class loader to load the resource with, if any
  66. * @param clazz the class to load resources with, if any
  67. */
  68. protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
  69. this.path = StringUtils.cleanPath(path);
  70. this.classLoader = classLoader;
  71. this.clazz = clazz;
  72. }
  73.  
  74. /**
  75. * Return the path for this resource (as resource path within the class path).
  76. */
  77. //返回资源路径
  78. public final String getPath() {
  79. return this.path;
  80. }
  81.  
  82. /**
  83. * Return the ClassLoader that this resource will be obtained from.
  84. */
  85. //这个资源得到的类加载器
  86. public final ClassLoader getClassLoader() {
  87. return (this.clazz != null ? this.clazz.getClassLoader() : this.classLoader);
  88. }
  89.  
  90. /**
  91. * This implementation checks for the resolution of a resource URL.
  92. * @see java.lang.ClassLoader#getResource(String)
  93. * @see java.lang.Class#getResource(String)
  94. */
  95. @Override
  96. public boolean exists() {
  97. return (resolveURL() != null);
  98. }
  99.  
  100. /**
  101. * Resolves a URL for the underlying class path resource.
  102. * @return the resolved URL, or {@code null} if not resolvable
  103. */
  104. //返回潜在的class 路径资源URL
  105. protected URL resolveURL() {
  106. if (this.clazz != null) {
  107. return this.clazz.getResource(this.path);
  108. }
  109. else if (this.classLoader != null) {
  110. return this.classLoader.getResource(this.path);
  111. }
  112. else {
  113. return ClassLoader.getSystemResource(this.path);
  114. }
  115. }
  116.  
  117. /**
  118. * This implementation opens an InputStream for the given class path resource.
  119. * @see java.lang.ClassLoader#getResourceAsStream(String)
  120. * @see java.lang.Class#getResourceAsStream(String)
  121. */
  122. //
  123. @Override
  124. public InputStream getInputStream() throws IOException {
  125. InputStream is;
  126. if (this.clazz != null) {
  127. is = this.clazz.getResourceAsStream(this.path);
  128. }
  129. else if (this.classLoader != null) {
  130. is = this.classLoader.getResourceAsStream(this.path);
  131. }
  132. else {
  133. is = ClassLoader.getSystemResourceAsStream(this.path);
  134. }
  135. if (is == null) {
  136. throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
  137. }
  138. return is;
  139. }
  140.  
  141. /**
  142. * This implementation returns a URL for the underlying class path resource,
  143. * if available.
  144. * @see java.lang.ClassLoader#getResource(String)
  145. * @see java.lang.Class#getResource(String)
  146. */
  147. @Override
  148. public URL getURL() throws IOException {
  149. URL url = resolveURL();
  150. if (url == null) {
  151. throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
  152. }
  153. return url;
  154. }
  155.  
  156. /**
  157. * This implementation creates a ClassPathResource, applying the given path
  158. * relative to the path of the underlying resource of this descriptor.
  159. * @see org.springframework.util.StringUtils#applyRelativePath(String, String)
  160. */
  161. @Override
  162. public Resource createRelative(String relativePath) {
  163. String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
  164. return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
  165. }
  166.  
  167. /**
  168. * This implementation returns the name of the file that this class path
  169. * resource refers to.
  170. * @see org.springframework.util.StringUtils#getFilename(String)
  171. */
  172. @Override
  173. public String getFilename() {
  174. return StringUtils.getFilename(this.path);
  175. }
  176.  
  177. /**
  178. * This implementation returns a description that includes the class path location.
  179. */
  180. @Override
  181. public String getDescription() {
  182. StringBuilder builder = new StringBuilder("class path resource [");
  183. String pathToUse = path;
  184. if (this.clazz != null && !pathToUse.startsWith("/")) {
  185. builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
  186. builder.append('/');
  187. }
  188. if (pathToUse.startsWith("/")) {
  189. pathToUse = pathToUse.substring(1);
  190. }
  191. builder.append(pathToUse);
  192. builder.append(']');
  193. return builder.toString();
  194. }
  195.  
  196. /**
  197. * This implementation compares the underlying class path locations.
  198. */
  199. @Override
  200. public boolean equals(Object obj) {
  201. if (obj == this) {
  202. return true;
  203. }
  204. if (obj instanceof ClassPathResource) {
  205. ClassPathResource otherRes = (ClassPathResource) obj;
  206. return (this.path.equals(otherRes.path) &&
  207. ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) &&
  208. ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz));
  209. }
  210. return false;
  211. }
  212.  
  213. /**
  214. * This implementation returns the hash code of the underlying
  215. * class path location.
  216. */
  217. @Override
  218. public int hashCode() {
  219. return this.path.hashCode();
  220. }
  221.  
  222. }

Spring--ClassPathResource的更多相关文章

  1. 深入了解 Java Resource && Spring Resource

    在Java中,为了从相对路径读取文件,经常会使用的方法便是: xxx.class.getResource(); xxx.class.getClassLoader().getResource(); 在S ...

  2. ajax图片上传及FastDFS入门案例.

    今天来开始写图片上传的功能, 现在的图片上传都讲求 上传完成后立刻回显且页面不刷新, 这里到底是怎么做的呢? 当然是借助于ajax了, 但是ajax又不能提交表单, 这里我们还要借助一个插件: jqu ...

  3. JdbcTemplate源码解析

    先写一个测试代码 package jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arr ...

  4. Spring之ClassPathResource加载资源文件

    先看Demo: 1 @Test 2 public void testClassPathResource() throws IOException { 3 Resource res = new Clas ...

  5. 8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource

    8.3.1 Resource实现类------ClassPathResource : 访问类加载路径下的资源的实现类 2.访问类加载路径下的资源 ClassPathResource 用来访问类加载路径 ...

  6. 学习AOP之透过Spring的Ioc理解Advisor

    花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...

  7. 【初探Spring】------Spring IOC(三):初始化过程---Resource定位

    我们知道Spring的IoC起到了一个容器的作用,其中装得都是各种各样的Bean.同时在我们刚刚开始学习Spring的时候都是通过xml文件来定义Bean,Spring会某种方式加载这些xml文件,然 ...

  8. 【初探Spring】------Spring IOC(二):初始化过程---简介

    首先我们先来看看如下一段代码 ClassPathResource resource = new ClassPathResource("bean.xml"); DefaultList ...

  9. 【初探Spring】------Spring IOC(一)

    IOC:Inversion of Control(控制反转).IOC它所体现的并不是一种技术,而是一种思想,一种将设计好的对象交给容器来管理的思想.IOC的核心思想就体现在控制.反转这两个词上面,要理 ...

  10. spring笔记5 spring IOC的基础知识1

    1,ioc的概念 Inverse of control ,控制反转,实际的意义是调用类对接口实现类的依赖,反转给第三方的容器管理,从而实现松散耦合: ioc的实现方式有三种,属性注入,构造函数注入,接 ...

随机推荐

  1. [django]urls.py 中重定向

    Django 1.5 有时候需要对一个链接直接重定向,比如首页啥的重定向到一个内容页等等,在views.py 中可以设定,如果没有参数啥的在urls.py 中设定更加方面 from django.vi ...

  2. wget 常用参数释义

    wget 大法好啊,废话不多说,下面开始wget之旅吧. 下载限速 wget命令有一个内建的选项可以先顶下载任务占有的最大的带宽,从而保证其他应用程序的流畅运行. 具体使用--limit-rate 数 ...

  3. Android简易实战教程--第二十六话《网络图片查看器在本地缓存》

    本篇接第二十五话  点击打开链接   http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...

  4. Linux命令—压缩及其他

     (1)为了更好的传送和保存文件,需要对某些文件和目录进行压缩和解压缩操作,Linux 提供了强大的压缩.解压缩命令,常用的tar命令. (2)在Linux中,如果要使用储存设备(硬盘.光驱.移动 ...

  5. 一套强大的vim配置文件+详细注释

    phpchina折腾王独家配置,灰常牛叉的一套vim配置,另附有详细注释,自己折腾vim的时候可以参照其中的大部分设置进行一些个性化定制."是否兼容VI,compatible为兼容,noco ...

  6. iOS开发之使用block块进行数据遍历的方法

    看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...

  7. 6.0、Android Studio性能优化工具

    显示图像包含四个步骤.简单来说,CPU对比显示列表,GPU渲染图片显示,内存存储图片和数据,电池提供点力能源.每个部分的硬件都有限制,超过这个限制会导致应用运行较慢,显示性能差,或者耗电. 为了查找造 ...

  8. 后端分布式系列:分布式存储-HDFS Client 设计实现解析

    前面对 HDFS NameNode 和 DataNode 的架构设计实现要点做了介绍,本文对 HDFS 最后一个主要构成组件 Client 做进一步解析. 流式读取 HDFS Client 为客户端应 ...

  9. Android Service详解

    service作为四大组件值得我们的更多的关注 在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务.例如,一个从service播放音乐的音乐播放器,应 ...

  10. 【UNIX网络编程第三版】阅读笔记(一):代码环境搭建

    粗略的阅读过<TCP/IP详解>和<计算机网络(第五版)>后,开始啃这本<UNIX网络编程卷一:套接字联网API>,目前linux下的编程不算太了解,在阅读的过程中 ...