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

你可能感兴趣的:(SSH)