Resource

Resource 接口为处理和访问不同类型资源(如文件、URL、输入流等)提供了统一的 API,支持资源的存在性检查、读取、转换等操作。

public interface Resource extends InputStreamSource {boolean exists();default boolean isReadable() {return exists();}default boolean isOpen() {return false;}default boolean isFile() {return false;}URL getURL() throws IOException;URI getURI() throws IOException;File getFile() throws IOException;default ReadableByteChannel readableChannel() throws IOException {return Channels.newChannel(getInputStream());}default byte[] getContentAsByteArray() throws IOException {return FileCopyUtils.copyToByteArray(getInputStream());}default String getContentAsString(Charset charset) throws IOException {return FileCopyUtils.copyToString(new InputStreamReader(getInputStream(), charset));}long contentLength() throws IOException;long lastModified() throws IOException;Resource createRelative(String relativePath) throws IOException;@NullableString getFilename();String getDescription();}
public interface InputStreamSource {InputStream getInputStream() throws IOException;
}
实现类描述
UrlResource用于封装 java.net.URL,可以访问任何通过 URL 访问的资源,如文件、HTTPS、FTP 等。
ClassPathResource用于从类路径加载资源,支持使用类加载器或特定类来加载资源。
FileSystemResource基于 java.io.File 的实现,支持文件系统路径,采用 Java NIO API 进行操作。
PathResource基于 java.nio.file.Path 的实现,采用 NIO API,支持文件和 URL 解析,且实现了 WritableResource 接口。
ServletContextResource用于 ServletContext 资源,解析相对路径并在 Web 应用的根目录下查找资源。
InputStreamResource封装已打开的 InputStream,适用于流式访问已打开的资源,避免多次读取。
ByteArrayResource基于给定字节数组的实现,通过 ByteArrayInputStream 来加载内容,适合加载内存中的数据。

ResourceLoader

ResourceLoader 接口用于资源加载策略,通常在 Spring 应用中用于加载文件、类路径或其他类型的资源。它的子接口和实现类则为其功能扩展,支持不同类型的应用上下文或资源解析需求。|

public interface ResourceLoader {/** Pseudo URL prefix for loading from the class path: "classpath:". */String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;Resource getResource(String location);@NullableClassLoader getClassLoader();}

DefaultResourceLoader

DefaultResourceLoader 是 Spring 中用于加载各种类型资源的默认实现,支持通过类路径、文件系统、URL 等方式加载,并允许扩展协议解析器。

// Direct Known Subclasses:
// AbstractApplicationContext, ClassRelativeResourceLoader, FileSystemResourceLoader, ServletContextResourceLoader
public class DefaultResourceLoader implements ResourceLoader {@Nullableprivate ClassLoader classLoader;private final Set<ProtocolResolver> protocolResolvers = new LinkedHashSet<>(4);private final Map<Class<?>, Map<Resource, ?>> resourceCaches = new ConcurrentHashMap<>(4);public DefaultResourceLoader() {}public DefaultResourceLoader(@Nullable ClassLoader classLoader) {this.classLoader = classLoader;}public void addProtocolResolver(ProtocolResolver resolver) {Assert.notNull(resolver, "ProtocolResolver must not be null");this.protocolResolvers.add(resolver);}@SuppressWarnings("unchecked")public <T> Map<Resource, T> getResourceCache(Class<T> valueType) {return (Map<Resource, T>) this.resourceCaches.computeIfAbsent(valueType, key -> new ConcurrentHashMap<>());}public void clearResourceCaches() {this.resourceCaches.clear();}@Overridepublic Resource getResource(String location) {Assert.notNull(location, "Location must not be null");for (ProtocolResolver protocolResolver : getProtocolResolvers()) {Resource resource = protocolResolver.resolve(location, this);if (resource != null) {return resource;}}if (location.startsWith("/")) {return getResourceByPath(location);}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 = ResourceUtils.toURL(location);return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));}catch (MalformedURLException ex) {// No URL -> resolve as resource path.return getResourceByPath(location);}}}protected Resource getResourceByPath(String path) {return new ClassPathContextResource(path, getClassLoader());}protected static class ClassPathContextResource extends ClassPathResource implements ContextResource {public ClassPathContextResource(String path, @Nullable ClassLoader classLoader) {super(path, classLoader);}@Overridepublic String getPathWithinContext() {return getPath();}@Overridepublic Resource createRelative(String relativePath) {String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);return new ClassPathContextResource(pathToUse, getClassLoader());}}
}

ResourcePatternResolver

ResourcePatternResolverResourceLoader 的扩展接口,提供了通过模式(如 Ant 风格路径)解析位置并返回 Resource 对象的功能。它支持 classpath*: 前缀来获取类路径和模块路径中所有匹配的资源。

// "classpath:META-INF/config.xml" 只匹配 第一个 META-INF/config.xml
// "classpath*:META-INF/config.xml" 匹配 所有 JAR 包 和 所有 META-INF 目录 下的 config.xml// PathMatchingResourcePatternResolver
public interface ResourcePatternResolver extends ResourceLoader {String CLASSPATH_ALL_URL_PREFIX = "classpath*:";Resource[] getResources(String locationPattern) throws IOException;}

ResourceEditor

ResourceEditor 通过继承 PropertyEditorSupport 提供了对资源路径的自动解析和加载功能。它能够将字符串路径转换为 Resource 类型,同时支持占位符解析和动态路径处理。这使得资源的配置和加载变得更加灵活与便捷,尤其适用于需要根据配置或环境动态加载资源的场景。

public class ResourceEditor extends PropertyEditorSupport {private final ResourceLoader resourceLoader;@Nullableprivate PropertyResolver propertyResolver;private final boolean ignoreUnresolvablePlaceholders;public ResourceEditor() {this(new DefaultResourceLoader(), null);}public ResourceEditor(ResourceLoader resourceLoader, @Nullable PropertyResolver propertyResolver) {this(resourceLoader, propertyResolver, true);}public ResourceEditor(ResourceLoader resourceLoader, @Nullable PropertyResolver propertyResolver,boolean ignoreUnresolvablePlaceholders) {Assert.notNull(resourceLoader, "ResourceLoader must not be null");this.resourceLoader = resourceLoader;this.propertyResolver = propertyResolver;this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;}@Overridepublic void setAsText(String text) {if (StringUtils.hasText(text)) {String locationToUse = resolvePath(text).trim();setValue(this.resourceLoader.getResource(locationToUse));}else {setValue(null);}}protected String resolvePath(String path) {if (this.propertyResolver == null) {this.propertyResolver = new StandardEnvironment();}return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :this.propertyResolver.resolveRequiredPlaceholders(path));}@Override@Nullablepublic String getAsText() {Resource value = (Resource) getValue();try {return (value != null ? value.getURL().toExternalForm() : "");}catch (IOException ex) {return null;}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/89022.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/89022.shtml
英文地址,请注明出处:http://en.pswp.cn/bicheng/89022.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring Boot - Spring Boot 集成 MyBatis 分页实现 PageHelper

一、PageHelper 概述 PageHelper 是一个优秀的 MyBatis 分页插件&#xff0c;可以方便地在 Spring Boot 项目中使用 MyBatis 结合 PageHelper 实现分页功能二、PageHelper 引入 1、依赖引入 pom.xml <properties>...<postgresql.verison>42.5.6</postgresql.ver…

jenkins自动化部署前端vue+docker项目

文章目录一、准备工作二、编写dockerfile文件三、新建jenkins任务一、准备工作 默认你的服务器centos已经搭建完成&#xff0c;同时已经安装了jenkins和docker。 接下来去下载开源项目ruoyi并上传到自己的gitee中。 二、编写dockerfile文件 打开项目工程&#xff0c;在rouy…

opencv中contours的使用

一 Contour FindingContours使用 STL-style vector<> 表示&#xff0c;如 vector<cv::Point>, vector<cv::Point2f>。opencv中&#xff0c;使用函数 cv::findContours() 寻找contours&#xff0c; 具体函数定义如下&#xff1a;void cv::findContours(cv::In…

网络安全初级

1、docker并配置代理 &#xff08;1&#xff09;在Ubuntu中安装docker apt-get install docker.io docker-compose &#xff08;2&#xff09;安装完成后&#xff0c;进入/etc/systemd/system/docker.service.d/http-proxy.conf配置文件下进行代理的配置&#xff0c;配置如图…

JetBrains IDE 性能优化指南:idea.vmoptions 核心参数解析与配置建议

文章目录深入解析 JetBrains IDE 的 VM 选项&#xff1a;idea.vmoptions 参数详解一、内存与垃圾回收配置二、诊断与错误处理三、运行时优化参数四、模块系统与反射控制五、特殊参数说明六、配置建议指南深入解析 JetBrains IDE 的 VM 选项&#xff1a;idea.vmoptions 参数详解…

Datawhale AI夏令营 《基于带货视频评论的用户洞察挑战赛》Part .1.

1. 赛题 参赛者需要构建端到端的评论分析系统&#xff0c;完成三大核心任务&#xff1a; 商品识别 输入&#xff1a;视频描述文本(video_desc)和标签(video_tags)输出&#xff1a;精准识别推广商品名称(Xfaiyx Smart Translator/Recorder) 多维情感分析 维度&#xff1a;情感倾…

【博文汇项目全维度测试报告:功能与自动化双轨验证】

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” 文章目录 项目背景:项目背景与意义&#xff1a;项目概述已实现的主要功能包括&#xff1a;当前系统存在的不足…

Java陷阱之assert关键字详解

Assert.isTrue()方法用于断言条件是否为真&#xff0c;如果条件不满足&#xff08;即为false&#xff09;&#xff0c;则会抛出IllegalArgumentException&#xff0c;并附带预设的错误信息。在示例中&#xff0c;当1.23不小于2.23时&#xff0c;方法抛出了异常&#xff0c;显示…

mysql 散记:innodb引擎和memory引擎对比 sql语句少用函数 事务与长事务

文章目录innodb引擎和memory引擎对比对比sql 语句&#xff1a;尽可能不使用函数条件隐式转换隐式类型转换隐式字符编码转换补充问题事务与长事务ACIDread viewMVCC 一致性视图当前读view 虚拟表长事务的影响与排查影响排查方法预防innodb引擎和memory引擎对比 innodb引擎是索引…

APK安装器(安卓端)一键解除VX限制!轻松安装各种手机应用

VX为了防止恶意软件通过平台传播&#xff0c;保障用户设备安全&#xff0c;会把通过VX发送的 APK 文件自动改成 “apk.1” 格式&#xff0c;这样就能减少恶意软件传播的风险。我平时推荐安卓软件的时候&#xff0c;有朋友反馈说&#xff0c;文件发到VX里就变成 “apk.1” 了&am…

Debian:从GNOME切换到Xfce

最近为20年前的T43重新安装了Debian系统&#xff0c;但使用的gnome桌面太卡了。于是换成轻量级的Xfce系统。 1.安装Xfce sudo apt update sudo apt install xfce4 xfce4-goodies命令中xfce4 是Xfce桌面环境的核心组件&#xff0c;xfce4-goodies 是一些额外的工具和插件&#xf…

徐州服务器租用:BGP线路的特点有哪些?

BGP的中文全称为边界网关协议&#xff0c;是指一种运行在传输控制协议上的自治系统路由协议&#xff0c;主要的功能就是可以实时控制路由的传播&#xff0c;同时能够帮助用户选择更合适的路由线路&#xff0c;保证网络能够稳定的运行&#xff0c;不会轻易出现网络卡顿或故障的状…

Java使用OSHI获取服务器信息

OSHI可以获取系统信息&#xff08;CPU、内存、磁盘、网络等&#xff09;&#xff0c;纯Java实现&#xff08;通过JNA访问本地API&#xff0c;无需安装本地库&#xff09;&#xff0c;跨平台支持。引入依赖<dependency><groupId>com.github.oshi</groupId><…

企业数字化资产管理安全、成本、协作困局难解?

在数字化浪潮席卷全球的今天&#xff0c;3D技术已成为驱动影视动画、工业设计、建筑可视化等领域创新的核心动力。然而&#xff0c;随着3D资产规模呈指数级增长&#xff0c;企业正面临前所未有的管理挑战&#xff1a;海量模型存储混乱、版本迭代难以追溯、团队协作效率低下、知…

力扣面试150题--组合总和

Day 72 题目描述&#xff08;终于理顺回溯了&#xff09;思路 这里还是基于模板来说明代码思路void backtracking(参数) {if (终止条件) {存放结果;return;}for (选择 : 本层集合中的元素) {处理节点;backtracking(路径, 选择列表); // 递归撤销处理; // 回溯} }对于主要函数的…

多客户端-服务器(select,poll)

多客户端-服务器结构总结一、普通CS架构的局限性核心问题&#xff1a;单线程中accept&#xff08;阻塞等待连接&#xff09;与read&#xff08;阻塞读取数据&#xff09;函数互相干扰&#xff0c;无法同时处理多客户端。本质原因&#xff1a;阻塞型函数需独立执行&#xff0c;若…

如何使用postman做接口测试?

&#x1f345; 点击文末小卡片 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 常用的接口测试工具主要有以下几种&#xff1a;Postman: 简单方便的接口调试工具&#xff0c;便于分享和协作。具有接口调试&#xff0c;接口集管理&#xff0c…

新型网络架构设计助力智慧医疗降本增效

随着智慧医疗的快速发展,越来越多的医院开始布局“互联网+医疗”服务,通过数字化手段提升医疗服务效率。然而,如何构建一个既稳定可靠又具备灵活扩展能力的医疗网络,成为医院数字化转型中的关键问题。本文以某智慧医疗项目为例,探讨传统网络与SD-WAN结合的最佳实践。 背景…

一文读懂现代卷积神经网络—使用块的网络(VGG)

目录 什么是使用块的网络&#xff08;VGG&#xff09;&#xff1f; 一、VGG 的核心思想&#xff1a;用块&#xff08;Block&#xff09;构建网络 二、VGG 的网络结构 三、VGG 的优势 1. 结构简洁统一 2. 强大的特征表达能力 3. 小卷积核的计算效率 4. 良好的迁移学习性…

Linux的相关学习

linux 1.文件权限怎么修改 chmod [权限模式] [文件或目录]1、**数字模式&#xff08;八进制&#xff09;**&#xff1a; chmod 755 myfile.sh # 所有者&#xff1a;rwx (7)&#xff0c;组&#xff1a;r-x (5)&#xff0c;其他用户&#xff1a;r-x (5) 7 rwx&#xff08;读写…