接口简介

JDK中提供了java.net.URL这个类来用于获取不同种类的资源(根据不同前缀的url可以获取不同种类的资源)。但是URL这个类没有获取classpath和ServletContext下的资源的能力。因此Spring提供了Resource接口,用于获取各种资源。

spring中的org.springframework.core.io.Resource接口代表着物理存在的任何资源,其继承于org.springframework.core.io.InputStreamSource。

Spring的资源接口是一个更强大的接口,用于抽象对低级资源的访问。

Resource接口的主要方法如下:


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());
}
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String relativePath) throws IOException;
@Nullable
String getFilename();
String getDescription();
}

关于上面提供的getURI和getURL方法这边做下解释。URI用于唯一指定互联网上一个资源的名字,我们可以理解为一个资源的唯一id。URL代表一个资源的唯一地址,我们通过这个地址可以在互联网上寻找到这个资源。通常可以用URL来代替URI。

Resource继承了InputStreamSource接口,下面是其定义:

public interface InputStreamSource {

    InputStream getInputStream() throws IOException;

}

Resource的比较重要的方法如下:

  • getInputStream(): 定位和打开resource, 返回InputStream 来读取资源。每一次调用都会返回一个新的InputStream,调用者负责将其关闭。
  • exists(): 返回boolean,用来判断资源是否存在
  • isOpen(): 返回boolean,用来判断资源是不是已经存在一个open stream处理器。 true表明InputStream不能被多次读取,那么这次的读取会被关闭,以避免资源泄露。false是所有正常资源实现的返回,有可能会抛异常:InputStreamResource。
  • getDescription(): 返回资源的描述,用于错误输出。通常这会返回resource URL的全名。

其他的方法可以让你获取到代表resource的URL或者File对象。

Resource接口在Spring代码中非常常用,你也可以考虑应用到你的程序中。

内置的Resource实现

Spring中提供了很多Resource接口的实现类。主要有ByteArrayResource, ClassPathResource, DescriptiveResource, FileSystemResource, InputStreamResource, PortletContextResource, ServletContextResource和UrlResource。常用的有:

  • ClassPathResource:通过 ClassPathResource 以类路径的方式进行访问;
  • FileSystemResource:通过 FileSystemResource 以文件系统绝对路径的方式进行访问;
  • ServletContextResource:通过 ServletContextResource 以相对于Web应用根目录的方式进行访问。
  • UrlResource :通过java.net.URL来访问资源,当然它也支持File格式,如“file:”、“http:”。

ResourceLoader接口

ResourceLoader接口用来加载Resource资源。

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

所有的ApplicationContext类都实现了ResourceLoader接口,所以我们可以使用context来加载resource。

不同类型的ApplicationContext会返回不同的Resource。

当你在特定的应用程序上下文上调用getResource(),并且指定的位置路径没有特定的前缀时,你将返回适合该特定应用程序上下文的资源类型。例如,假设对ClassPathXmlApplicationContext实例执行了以下代码片段:

Resource template = ctx.getResource("some/resource/path/myTemplate.txt");

ClassPathXmlApplicationContext返回ClassPathResource,FileSystemXmlApplicationContext返回FileSystemResource,WebApplicationContext返回ServletContextResource。 他会返回和ApplicationContext相对应的Resource实现。

当然,你可以强制ClassPathResource使用,而不管ApplicationContext到底是什么。使用的方法就是在资源路径前面加前缀。

Resource template1 = ctx.getResource("classpath:some/resource/path/myTemplate.txt");

Resource template2 = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");

Resource template3 = ctx.getResource("file:///some/resource/path/myTemplate.txt");

Resource[] template4 = ctx.getResources("file:///some/resource/path/my**a.txt");

ResourceLoaderAware接口

ResourceLoaderAware接口是一个特殊的回调接口,表明该组件需要提供一个ResourceLoader的引用。 下面是ResourceLoaderAware的定义:

public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader resourceLoader);
}

一个Bean如果实现了ResourceLoaderAware接口,容器在加载Bean的时候会给这个Bean注入一个ResourceLoad实现类(容器设置的ResourceLoad就是容器本身,因为所有的Spring application contexts 都实现了ResourceLoader 接口),利用这个实现类也可以加载Resource资源。

Resources as Dependencies

不知道在讲什么,后续再整理。。。

构造 ApplicationContext

//从classpath下寻找配置文件构造ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
//从当前工作目录寻找配置文件构造FileSystemXmlApplicationContext
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
//由于加了classpath前缀,所以从classpath下寻找配置构造FileSystemXmlApplicationContext
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");
//以MessengerService类所在的路径为基准路径,寻找services.xml和daos.xml作为配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}, MessengerService.class);

通配符形式构造资源

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");

Spring系列.Resource接口的更多相关文章

  1. Spring系列.Environment接口

    Environment 接口介绍 在 Spring 中,Environment 接口主要管理应用程序两个方面的内容:profile 和 properties. profile 可以简单的等同于环境,比 ...

  2. Spring中Resource接口的前缀书写格式

    Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");   //这个 ...

  3. 攻城狮在路上(贰) Spring(三)--- Spring 资源访问利器Resource接口

    Spring为了更好的满足各种底层资源的访问需求.设计了一个Resource接口,提供了更强的访问底层资源的能力.Spring框架使用Resource装载各种资源,包括配置文件资源.国际化属性文件资源 ...

  4. Spring系列(零) Spring Framework 文档中文翻译

    Spring 框架文档(核心篇1和2) Version 5.1.3.RELEASE 最新的, 更新的笔记, 支持的版本和其他主题,独立的发布版本等, 是在Github Wiki 项目维护的. 总览 历 ...

  5. Resource接口,及资源

    Resource介绍 编码的时候,除了代码本身,我们还需要对外部的资源进行处理.例如:URL资源.URI资源.File资源.ClassPath相关资源.服务器相关资源(VFS等)等等. 而这些资源的处 ...

  6. 【Java】Spring之Resource(三)

    Java的各种URL前缀的标准类和标准处理程序不足以完全访问低级资源.例如,没有URL可用于访问需要从类路径或相对于a获取的资源的标准化实现 ServletContext.虽然可以为专用URL 前缀注 ...

  7. Spring系列18:Resource接口及内置实现

    本文内容 Resource接口的定义 Resource接口的内置实现 ResourceLoader接口 ResourceLoaderAware 接口 Resource接口的定义 Java 的标准 ja ...

  8. Spring资源访问接口Resource

    该接口拥有对不同资源类型的实现类 boolean exists() 资源是否存在 boolean isOpen() 资源是否打开 URL getURL() 如果底层资源可以表示成URL,则该方法返回对 ...

  9. Spring源码系列 — Resource抽象

    前言 前面两篇介绍了上下文的启动流程和Environemnt的初始化,这两部分都是属于上下文自身属性的初始化.这篇开始进入Spring如何加载实例化Bean的部分 - 资源抽象与加载. 本文主要从以下 ...

随机推荐

  1. jvm系列四类加载与字节码技术

    四.类加载与字节码技术 1.类文件结构 首先获得.class字节码文件 方法: 在文本文档里写入java代码(文件名与类名一致),将文件类型改为.java java终端中,执行javac X:...\ ...

  2. Jenkins(6)测试报告邮件发送

    前言 前面已经实现在jenkins上展示html的测试报告,接下来只差最后一步,把报告发给你的领导,展示你的劳动成果了. 安装 Email Extension Plugin 插件 jenkins首页- ...

  3. Python爬虫笔记一(来自MOOC) Requests库入门

    Python爬虫笔记一(来自MOOC) 提示:本文是我在中国大学MOOC里面自学以及敲的一部分代码,纯一个记录文,如果刚好有人也是看的这个课,方便搬运在自己电脑上运行. 课程为:北京理工大学-嵩天-P ...

  4. 3. Linear Regression with Multiple Variables

    前面还有一章主要讲解,基本的Linear Algebra线性代数的知识,都比较简单,这里就直接跳过了. Speaker: Andrew Ng 1. Multiple featues 训练集的特征变成了 ...

  5. Codeforces Round #653 (Div. 3)

    比赛链接:https://codeforces.com/contest/1374 A. Required Remainder 题意 给出 $x, y, n$,找到最大的整数 $0 \le k \le ...

  6. Codeforces Round #697 (Div. 3) G. Strange Beauty (DP,数学)

    题意:给你一组数,问你最少删去多少数,使得剩下的数,每个数都能整除数组中其它某个数或被数组中其它某个数整除. 题解:我们直接枚举所有因子,\(dp[i]\)表示\(i\)在数组中所含的最大因子数(当我 ...

  7. Codeforces Global Round 12 D. Rating Compression (思维,双指针)

    题意:给你一长度为\(n\)的数组,有一长度为\(k\ (1\le k \le n)\)的区间不断从左往右扫过这个数组,总共扫\(n\)次,每次扫的区间长度\(k=i\),在扫的过程中,每次取当前区间 ...

  8. zoj3905 Cake

    Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One day, ...

  9. 洛谷 P2391.白雪皑皑 (并查集,思维)

    题意:有\(n\)个点,对这些点进行\(m\)次染色,第\(i\)次染色会把区间\((i*p+q)\ mod\ N+1\)和\((i*q+p)\ mod\ N+1\)之间的点染成颜色\(i\),问最后 ...

  10. javascript——function类型(this关键字)

    如果不用分组的话,当用exec检测rar的时候会错误 结果: Function: 返回值为三(不推荐)