ClassPathResource类的继承关系

InputStreamSource<--Resource<--AbstractResource<---AbstractFileResolvingResource<--ClassPathResource

public ClassPathResource(String path) {
                      this(path, (ClassLoader) null);
       }

public ClassPathResource(String path, ClassLoader classLoader) {
   Assert.notNull(path, "Path must not be null");
   String pathToUse = StringUtils.cleanPath(path);
   if (pathToUse.startsWith("/")) {
    pathToUse = pathToUse.substring(1);
}
this.path = pathToUse;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}

public ClassPathResource(String path, Class<?> clazz) {
Assert.notNull(path, "Path must not be null");
this.path = StringUtils.cleanPath(path);
this.clazz = clazz;
}

protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
this.path = StringUtils.cleanPath(path);
this.classLoader = classLoader;
this.clazz = clazz;
}

可以发现,该类有两个接口,InputStream ,Resource,两个抽象类AbstractResource ,AbstractFileResolvingResourc,一个非抽象类ClassPathResource,所以用只能用ClassPathResource的实例(因为父类都是抽象的)

***********************************************************************************************************************************************************

UrlResource的继承关系

InputStreamSource<--Resource<--AbstractResource<---AbstractFileResolvingResource<-UrlResource

public UrlResource(URL url) {
Assert.notNull(url, "URL must not be null");
this.url = url;
this.cleanedUrl = getCleanedUrl(this.url, url.toString());
this.uri = null;
}

public UrlResource(URI uri) throws MalformedURLException {
Assert.notNull(uri, "URI must not be null");
this.url = uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, uri.toString());//通过观察源代码,其具体实现也是用了StringUtils中的cleanPath来处理的
this.uri = uri;
}

public UrlResource(String path) throws MalformedURLException {
Assert.notNull(path, "Path must not be null");
this.url = new URL(path);
this.cleanedUrl = getCleanedUrl(this.url, path);
this.uri = null;
}

***********************************************************************************************************************

FileSystemResource类的继承关系

InputStreamSource<--Resource<--AbstractResource<--FIleSystemResource(也实现了WritableResource接口)

注意其构造器

public FileSystemResource(File file) {
Assert.notNull(file, "File must not be null");
this.file = file;
this.path = StringUtils.cleanPath(file.getPath());
}

public FileSystemResource(String path) {
Assert.notNull(path, "Path must not be null");
this.file = new File(path);
this.path = StringUtils.cleanPath(path);
}

说明由于该类封装了File,故可以用来处理文件

*************************************************************************************************************************************************

InputStreamResource的继承关系

InputStreamSource<--Resource<--AbstractResource<--InputStreamResource

其构造器

public InputStreamResource(InputStream inputStream) {
this(inputStream, "resource loaded through InputStream");
}

public InputStreamResource(InputStream inputStream, String description) {
if (inputStream == null) {
throw new IllegalArgumentException("InputStream must not be null");
}
this.inputStream = inputStream;
this.description = (description != null ? description : "");
}

注意该类封装了InputStream,所以也可以当做输入流来使用

****************************************************************************************************

用到的设计模式:

抽象类可以实现接口的部分方法,另外一些方法并不实现(如果封装了算法或过程,里面使用的是接口方法,实现要到具体子类中才能确定,这就是Template设计模式),每一个模版方法,在最终的子类中都有具体的实现

有关Spring中Resource的继承关系(代码解读)相关推荐

  1. Spring中@Resource与@Autowired、@Qualifier的用法与区别

    Spring中@Resource与@Autowired.@Qualifier的用法与区别 1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法 ...

  2. 在Spring中使用JDBC访问关系数据

    在Spring中使用JDBC访问关系数据 本指南将引导您完成使用Spring访问关系数据的过程. 你会建立什么 您将构建一个使用Spring JdbcTemplate访问存储在关系数据库中的数据的应用 ...

  3. angularjs 中的scope继承关系——(2)

    转自:http://www.lovelucy.info/understanding-scopes-in-angularjs.html angularjs 中的scope继承关系 ng-include ...

  4. Spring容器父子类继承关系交给spring容器管理采用@autowired自动装配分析

    第一个青春是上帝给的 第二个的青春是靠自己努力的. 在开发项目过程中开发者们可能会遇到这样的问题 @autowired自动装配模式的工作模式是:在springIOC定位所有的Bean后,这个字段需要按 ...

  5. angularjs 中的scope继承关系——(1)

    转自:http://www.lovelucy.info/understanding-scopes-in-angularjs.html JavaScript 的原型链继承 假设父类 parentScop ...

  6. spring中resource设计与实现

    spring对资源及资源加载器作了抽象,资源包括可以文件.URL,URI等 1.类层次图

  7. spring中的CGLIB动态代理(代码)

    因为jdk带来具有局限性,使用动态代理的对象必须实现一个或多个接口,CGLIB代理不需要实现接口 UserDao.java目标类: package com.liu.cglib;//目标类 public ...

  8. spring中的jdk动态代理(代码步骤)

    UserDao.java接口: package com.liu.jdk;public interface UserDao {public void addUser();public void dele ...

  9. [夜寂]Spring中Resource接口有关的实现

    /*     Resource resource = new FileSystemResource("Beans.xml");         try {              ...

  10. java关于继承的代码_java编写动物世界的继承关系代码

    满意答案 bjtm0210 推荐于 2017.12.16 采纳率:57%    等级:8 已帮助:611人 我写了一个,内容比较简单的.代码如下:public class AnimalTest { A ...

最新文章

  1. python模块之json,pickle
  2. java正则表达式及api_JAVA常用API:正则表达式regular expression
  3. python简介怎么写-python爬虫简历怎么写
  4. CentOS压缩命令zip
  5. 深入理解java虚拟机一 JAVA运行时内存区域与class文件
  6. 从零学PyTorch:DataLoader构建高效的自定义数据集
  7. eclipse中设定文档注释
  8. python装饰器模式带参数_python 装饰器模式 我的理解
  9. Extmail maildrop错误
  10. linux 获取网站预览图,Shell脚本实现获取网页快照并生成缩略图 -电脑资料
  11. 如何使用JavaScript来写ASP程序
  12. flash 围棋_17岁攻读剑桥计算机,围棋只有业余一段,研发阿尔法狗战胜柯洁
  13. OMAPL138 DSP程序固化
  14. 数据处理的神来之笔 解决缓存击穿的终极利器
  15. 读书笔记——指数基金投资指南
  16. 实用工具系列 - FileZilla安装下载与使用
  17. nodejs addon
  18. 【Netty - 解码器】did not read anything but decoded a message 异常
  19. 人脸识别SVM算法实现--参考麦子学院彭亮机器学习基础5.2
  20. PHP汉字转拼音函数

热门文章

  1. C#:$符号和@符号的用法介绍
  2. tcp长连接java_聊聊 TCP 长连接和心跳那些事
  3. Java8 LocalDateTime和Date相互转换
  4. 一个简单的十年回顾及展望
  5. Global.asax取绝对路径
  6. ASP.NET MVC 4 笔记
  7. 20、淘宝技术这十年
  8. 使用localhost调试本地代码,setcookie无效
  9. Process Kill Technology Process Protection Against In Linux
  10. Android 使用URLConnection来post数据