[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,该类允许返回多次流,适合作为邮件附件的抽象资源类。

包装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

    ResourceLoader

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

    public interface ResourceLoader {
        Resource getResource(String location);
    

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

    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");