首页 > 其他分享 >[Spring]浅谈Spring的Resources体系

[Spring]浅谈Spring的Resources体系

时间:2023-09-21 14:05:16浏览次数:36  
标签:Resource 浅谈 URL Spring 资源 location new Resources String

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

[Spring]浅谈Spring的Resources体系_xml

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

[Spring]浅谈Spring的Resources体系_System_02

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


标签:Resource,浅谈,URL,Spring,资源,location,new,Resources,String
From: https://blog.51cto.com/u_15668812/7553305

相关文章

  • 框架分析(4)-Spring
    (框架分析(4)-Spring)专栏介绍link主要对目前市面上常见的框架进行分析和总结,希望有兴趣的小伙伴们可以看一下,会持续更新的。希望各位可以监督我,我们一起学习进步。SpringSpring框架是一个开源的Java企业级应用程序开发框架,它提供了一种简化Java开发的方法,帮助开发者构建可扩展......
  • 基于 COLA 架构的 Spring Cloud Alibaba(五)整合 Seata
    我们在使用微服务架构的时候,难免会遇到跨服务操作数据库的情形。例如,创建订单的业务,我们需要在订单服务生成订单数据,同时要在商品服务进行库存扣减。此时的操作涉及到订单、商品两个服务的数据操作,如何保证同时成功或同时失败呢?对此,本篇将介绍使用Seata来解决此问题。1.关于Sea......
  • springboot 的 properties 文件之间的联系
    application.properties为全局配置文件,默认加载这里的内容application-dev.properties为开发配置文件,会覆盖全局配置文件内容application-test.properties为测试配置文件,会覆盖全局配置文件application-prod.properties为线上配置文件,同上同样yaml与properties的相......
  • SpringCloudAlibaba整合Seata
    Seata(全称为SimpleExtensibleAutonomousTransactionArchitecture)是一个开源的分布式事务解决方案,用于解决分布式系统中的事务一致性问题。在分布式系统中,由于各个服务可能分布在不同的服务器上,涉及的数据库也可能不同,因此需要一种机制来保证分布式事务的原子性、一致性、隔离......
  • SpringCloudAlibaba整合Gateway
    在微服务架构中,加入网关(Gateway)是一种常见的模式,其作用是为了更好地管理和控制微服务的访问和通信。网关可以看作是微服务架构的入口,它位于客户端和内部微服务之间,充当了一个中间层的角色。以下是加入网关的作用:统一访问点:通过网关,客户端只需要与一个统一的访问点进行通信,而不用直......
  • SpringCloudAlibaba整合SkyWalking
    SkyWalking是一个开源的分布式系统性能监测和追踪解决方案。它主要用于监控和追踪分布式系统中的服务和应用,以便帮助开发人员和运维团队定位和解决性能问题和故障。以下是SkyWalking的主要作用:分布式追踪:SkyWalking可以跟踪分布式系统中各个组件之间的调用链,从而实现对整个系统的......
  • SpringCloudAlibaba整合Nacos配置中心
    Nacos可以帮助应用程序动态地管理和配置各种配置信息,例如应用程序的配置参数、数据库连接信息、日志级别、特性开关等。通过使用Nacos配置中心,可以将这些配置信息集中管理,而不需要将它们硬编码到应用程序中。这样做的好处是,当需要修改配置时,不需要重新打包和部署应用程序,而是直接......
  • springcloud搭建项目二nacos
    在微服务架构中,服务的数量可能会非常庞大,并且会动态地进行扩缩容。Nacos提供了服务发现功能,允许服务实例在注册到Nacos后,其他服务可以通过Nacos查询到它们的地址和信息,从而实现服务之间的通信和调用。当一个服务实例发生变化(比如新增、下线、失败等),Nacos能够及时感知这些变化,确保......
  • SpringCloudAlibaba整合OpenFeign
    OpenFeign是一个用于简化HTTP客户端的开源框架,它是SpringCloud生态系统的一部分。它的主要作用是简化在使用HTTP请求时编写客户端代码的过程,特别是在构建微服务架构中的服务之间进行通信时。OpenFeign提供了一种声明式的方式来定义和使用HTTP客户端接口。通过使用注解来描述HTTP......
  • springcloud项目搭建一
    我们先来搭建一个项目架子,后面再慢慢的往里面填充我们需要的各种组件首先我新建一个项目(FileNewProject) 创建我们的父工程,选择左边SprigInitializr类型的,注意几个箭头的位置,点击Next 啥也不选直接完成就好了,后面反正要改配置文件 生成后目录如下,如果有其它东西删了就......