有关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设计模式),每一个模版方法,在最终的子类中都有具体的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭梧悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值