有关Spring中Resource的继承关系(代码解读)
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的继承关系(代码解读)相关推荐
- Spring中@Resource与@Autowired、@Qualifier的用法与区别
Spring中@Resource与@Autowired.@Qualifier的用法与区别 1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法 ...
- 在Spring中使用JDBC访问关系数据
在Spring中使用JDBC访问关系数据 本指南将引导您完成使用Spring访问关系数据的过程. 你会建立什么 您将构建一个使用Spring JdbcTemplate访问存储在关系数据库中的数据的应用 ...
- angularjs 中的scope继承关系——(2)
转自:http://www.lovelucy.info/understanding-scopes-in-angularjs.html angularjs 中的scope继承关系 ng-include ...
- Spring容器父子类继承关系交给spring容器管理采用@autowired自动装配分析
第一个青春是上帝给的 第二个的青春是靠自己努力的. 在开发项目过程中开发者们可能会遇到这样的问题 @autowired自动装配模式的工作模式是:在springIOC定位所有的Bean后,这个字段需要按 ...
- angularjs 中的scope继承关系——(1)
转自:http://www.lovelucy.info/understanding-scopes-in-angularjs.html JavaScript 的原型链继承 假设父类 parentScop ...
- spring中resource设计与实现
spring对资源及资源加载器作了抽象,资源包括可以文件.URL,URI等 1.类层次图
- spring中的CGLIB动态代理(代码)
因为jdk带来具有局限性,使用动态代理的对象必须实现一个或多个接口,CGLIB代理不需要实现接口 UserDao.java目标类: package com.liu.cglib;//目标类 public ...
- spring中的jdk动态代理(代码步骤)
UserDao.java接口: package com.liu.jdk;public interface UserDao {public void addUser();public void dele ...
- [夜寂]Spring中Resource接口有关的实现
/* Resource resource = new FileSystemResource("Beans.xml"); try { ...
- java关于继承的代码_java编写动物世界的继承关系代码
满意答案 bjtm0210 推荐于 2017.12.16 采纳率:57% 等级:8 已帮助:611人 我写了一个,内容比较简单的.代码如下:public class AnimalTest { A ...
最新文章
- python模块之json,pickle
- java正则表达式及api_JAVA常用API:正则表达式regular expression
- python简介怎么写-python爬虫简历怎么写
- CentOS压缩命令zip
- 深入理解java虚拟机一 JAVA运行时内存区域与class文件
- 从零学PyTorch:DataLoader构建高效的自定义数据集
- eclipse中设定文档注释
- python装饰器模式带参数_python 装饰器模式 我的理解
- Extmail maildrop错误
- linux 获取网站预览图,Shell脚本实现获取网页快照并生成缩略图 -电脑资料
- 如何使用JavaScript来写ASP程序
- flash 围棋_17岁攻读剑桥计算机,围棋只有业余一段,研发阿尔法狗战胜柯洁
- OMAPL138 DSP程序固化
- 数据处理的神来之笔 解决缓存击穿的终极利器
- 读书笔记——指数基金投资指南
- 实用工具系列 - FileZilla安装下载与使用
- nodejs addon
- 【Netty - 解码器】did not read anything but decoded a message 异常
- 人脸识别SVM算法实现--参考麦子学院彭亮机器学习基础5.2
- PHP汉字转拼音函数