[Spring]浅谈Spring的Resources体系

Spring为什么要创建Resources体系

Java的标准java.net.url类和各种URL前缀的标准处理程序无法满足所有对low-level资源的访问.举个例子:没有标准化的URL实现类用于获取根据ServletContext的类路径。并且缺少某些Spring所需要的功能,例如检测某资源是否存在等。

Resource

Spring的Resource声明了访问low-level资源的能力。

public interface Resource extends InputStreamSource {
    // 确定此资源是否实际以物理形式存在。
    boolean exists();
    // 指示此资源是否代表具有开放流的句柄。
    // 如果为true,则不能多次读取InputStream,必须将其读取并关闭以避免资源泄漏。
    boolean isOpen();
    // 返回该资源的URL句柄
    URL getURL() throws IOException;
    // 获取该资源的File文件
    File getFile() throws IOException;
    // 根据相对路径创建该资源
    Resource createRelative(String relativePath) throws IOException;
    // 返回资源的文件名,例如:myfile.txt
    String getFilename();
    // 获取资源的描述
    String getDescription();
}

不仅如此,该接口还扩展了InputStreamSource接口,InputStreamSource接口只声明了一个方法。

public interface InputStreamSource {

    // 返回该资源的IO流,预计每次调用都会创建一次流。
    // 需要特别注意的是,当你使用JavaMail这类API的时候,要使用可以多次读取的流。
    // 记得关闭IO流
    InputStream getInputStream() throws IOException;

}

如果你需要仅读取一次性的流,Spring推荐使用InputStreamResource,如果需要访问多次流,那么选择ByteArrayResource,该类允许返回多次流,适合作为邮件附件的抽象资源类。

UML

Resource

Spring内置的Resource实现类

  • UrlResource

包装java.net.URL,提供可以通过该URL访问资源的能力,例如:File、Http、FTP等。
所有的URL都有标准的表达格式:file:表示访问文件系统路径,http:表示通过http协议获取的资源。
UrlResource通过构造函数传入一个URL,来进行访问,以下代码展示了如果通过URL访问资源文件并下载到本地磁盘中。

package com.xjm.resource;

import org.springframework.core.io.UrlResource;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;

/**
 * @author jaymin
 * 2020/12/6 20:56
 */
public class URLResourceDemo {
    public static void main(String[] args) throws IOException {
        UrlResource urlResource = new UrlResource("https://upload-images.jianshu.io/upload_images/19836894-f5744dda73c1dab8.png");
        String url = urlResource.getURL().toString();
        System.out.println(url);
        String filename = urlResource.getFilename();
        System.out.println(filename);
        String description = urlResource.getDescription();
        System.out.println(description);
        URI uri = urlResource.getURI();
        System.out.println(uri.toString());
        File file = new File("C:\\Users\\95152\\Desktop\\Git\\myPicture.png");
        try (InputStream inputStream = urlResource.getInputStream();
             OutputStream outStream = new FileOutputStream(file)) {
            byte[] buffer = new byte[8 * 1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, bytesRead);
            }
        }
    }
}
  • ClassPathResource

ClassPathResource表示从类路径获取的资源,通常使用线程上下文的ClassLoader进行资源加载.
我们的Web项目通常编译后,会将class文件存储在WEB-INF/classes下,Spring就可以通过ClassPathResource来访问这些文件.

package com.xjm.resource;

import org.springframework.core.io.ClassPathResource;

/**
 * @author jaymin
 * 2020/12/12 17:40
 */
public class ClassPathResourceDemo {
    public static void main(String[] args)  {
        ClassPathResource classPathResource = new ClassPathResource("spring-config.xml");
        System.out.println(classPathResource.getFilename());
        System.out.println(classPathResource.getDescription());
        System.out.println(classPathResource.getPath());
    }
}
  • FileSystemResource

java.io.File的一个包装,区别在于它是订制于Spring的Resource体系下的.

package com.xjm.resource;

import org.springframework.core.io.FileSystemResource;

import java.io.*;

/**
 * @author jaymin
 * 2020/12/12 18:00
 */
public class FileSystemResourceDemo {
    public static void main(String[] args) throws IOException {
        FileSystemResource fileSystemResource = new FileSystemResource("D:\\Spring\\spring-framework-5.1.x\\spring-demo\\src\\main\\java\\com\\xjm\\resource\\test.txt");
        if (!fileSystemResource.exists()) {
            return;
        }
        try (OutputStream outputStream = fileSystemResource.getOutputStream();
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream))) {
            bufferedWriter.append("I am using FileSystemResource of Spring");
            bufferedWriter.newLine();
            bufferedWriter.write("end.");
            bufferedWriter.flush();
        }
    }
}
  • ServletContextResource

这是ServletContext资源的Resource实现,它解析相关Web应用程序根目录中的相对路径。
它始终支持流(stream)访问和URL访问,但只有在扩展Web应用程序存档且资源实际位于文件系统上时才允许java.io.File访问。无论它是在文件系统上扩展还是直接从JAR或其他地方(如数据库)访问,实际上都依赖于Servlet容器。

  • InputStreamResource

返回一个仅一次使用的流,如果你要多次使用流,不要选择它

  • ByteArrayResource

对于任意的给定的字节数组进行内容加载。
具体的使用,参考我的RestTemplate的上传文件博客:Spring中如何通过form-data传输多文件数据

Ant-style Patterns

Spring不仅支持classpath:file:http:等各种前缀开头的资源文件解析,而且对于也支持Ant(路径匹配表达式)风格的通配符解析.

Pattern Description Example Remark
? 匹配任何的单个字符 example/?ork 可以匹配:example/fork;example/work
* 匹配0或者任意数量的字符 file:C:/some/path/*.xml 可以匹配C:/some/path下的所有xml文件
** 匹配0个或者更多的目录 classpath:com/mycompany/**/applicationContext.xml 可以匹配mycompany和applicationContext.xml的任意目录,例如:
classpath:com/mycompany/test/applicationContext.xml;
classpath:com/mycompany/work/applicationContext.xml.

ResourceLoader

用于加载资源的策略界面。提供此功能需要ApplicationContext以及扩展的ResourcePatternResolver支持。

public interface ResourceLoader {
    
    Resource getResource(String location);
}

对此,Spring还解释道:对于不同的容器实现类,如果你不指定特定的前缀,它返回不同的Resource实例。

Context Resource
ClassPathXmlApplicationContext ClassPathResource
FileSystemXmlApplicationContext FileSystemResource
WebApplicationContext ServletContextResource

资源加载策略:

Prefix Example Explanation
classpath: classpath:com/myapp/config.xml ClassPathResource
file: file:///data/config.xml 从文件系统中加载为URL,FileSystemResource
http: https://myserver/logo.png UrlResource
none /data/config.xml 根据具体的容器实现类来

UML

ResourceLoader

其中,ResourcePatternResolver就是支持Ant风格的接口层

DefaultResourceLoader

此类是ResourceLoader的默认实现类,它是一种策略模式的实践,根据传入的location的prefix来返回不同的Resource解析策略。你可以将DefaultResourceLoader看作是策略模式中的context角色

    /**
     * 获取Resource的具体实现类实例
     * @param location the resource location
     * @return
     */
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");
        // ProtocolResolver:用户自定义的协议资源解决策略
        for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
            Resource resource = protocolResolver.resolve(location, this);
            if (resource != null) {
                return resource;
            }
        }
        // 如果是"/"开头,则默认返回 ClassPathContextResource返回
        if (location.startsWith("/")) {
            return getResourceByPath(location);
        }
        // 如果是"classpath:"开头,则返回 ClassPathResource
        else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
        }
        else {
            try {
                // Try to parse the location as a URL...
                URL url = new URL(location);
                // 如果是系统文件,那么返回 FileUrlResource,否则返回 UrlResource
                return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
            }
            catch (MalformedURLException ex) {
                // No URL -> resolve as resource path.
                // 默认返回 ClassPathContextResource返回
                return getResourceByPath(location);
            }
        }
    }

ResourcePatternResolver

该接口定义了一个参数:CLASSPATH_ALL_URL_PREFIX,它是用来保证你可以使用ant风格进行资源的定位的。

public interface ResourcePatternResolver extends ResourceLoader {

    // 匹配所有符合classpath*:前缀的文件资源  
    // 与classpath:不同,它是默认返回第一个匹配的
    String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

    // 解析符合 ant风格 的资源
    Resource[] getResources(String locationPattern) throws IOException;

}

再谈ApplicationContext

ApplicationContext扩展了ResourcePatternResolver,也代表了它本身也具备解析资源文件的能力。在AbstractApplicationContext中,其构造函数就默认实例化了一个ResourcePatternResolver对象.将资源解析的任务委派给了DefaultResourceLoaderPathMatchingResourcePatternResolver来执行。

    // 实例化ResourcePatternResolver
    public AbstractApplicationContext() {
        this.resourcePatternResolver = getResourcePatternResolver();
    }
    
    protected ResourcePatternResolver getResourcePatternResolver() {
        return new PathMatchingResourcePatternResolver(this);
    }   

关于FileSystemResource的注意事项

  • 相对路径
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml");
  • 绝对路径
ApplicationContext ctx = new FileSystemXmlApplicationContext("/conf/context.xml");

Spring建议访问绝对路径的时候,增加"file:"作为前缀

ApplicationContext ctx = new FileSystemXmlApplicationContext("file:/bean.xml");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容