Resource接口
【转】https://blog.csdn.net/hbtj_1216/article/details/85487787
参考:官方文档
1 简介
Java
标准库中的java.net.URL
类和标准处理器对于处理低层的资源没有提供很好的功能。例如,并没有提供一个URL
的实现能够从classpath
或者ServletContext
中读取资源等等。因此,在Spring
中提供了这样一个Resource
接口,能够更加方便的读取各种资源。
2 Resource接口
Spring
提供的Resource
接口,是对第低层资源访问进行的一个抽象,提供能方便的使用。
下面是org.springframework.core.io.Resource
接口的定义:
public interface Resource extends InputStreamSource {
/**
* 判断资源在物理上是否存在
*/
boolean exists();
/**
* 表明该资源中的非空内容是否可以通过getInputStream()读取
*/
default boolean isReadable() {
return exists();
}
/**
* 表明该资源是否被一个打开的stream处理
*/
default boolean isOpen() {
return false;
}
/**
* 判断该资源是否代表文件系统中的一个文件
*/
default boolean isFile() {
return false;
}
/**
* 返回该资源的URL
*/
URL getURL() throws IOException;
/**
* 返回该资源的URI
*/
URI getURI() throws IOException;
/**
* 返回该资源对应的File
*/
File getFile() throws IOException;
/**
* 返回一个ReadableByteChannel(可读的字节流通道)
*/
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();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
Resource
接口继承的InputStreamSource
接口中海油一个非常重要的方法:
public interface InputStreamSource {
/**
* 找到并打开资源,返回读取资源内容的InputStream. 每次调用返回一个新的InputStream.
*/
InputStream getInputStream() throws IOException;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3 Spring中内置的Resource接口的实现类
Spring
中包含了如下几个Resource
接口的实现类:
- UrlResource:从
URL
获取资源。 - ClassPathResource:从
classpath
获取资源。 - FileSystemResource:从文件系统获取资源。
- ServletContextResource:从
servlet
上下文获取资源。 - InputStreamResource:从
InputStream
获取资源。 - ByteArrayResource:从字节数组获取资源。
UrlResource
UrlResource
是对java.net.URL
的封装,可以被用来访问任何可以通过URL
访问的资源对象,例如文件、HTTP
目标对象、FTP
目标对象等等。
每种类型的URL
都有表示该类型资源的前缀。例如file:
表示访问文件系统的URL
;http:
表示通过http
协议访问;ftp:
表示通过ftp
协议访问。
ClassPathResource
ClassPathResource
表示从classpath
中获取资源。
FileSystemResource
FileSystemResource
是对java.io.File
和java.nio.file.Path
的封装,代表从文件系统中读取文件。
ServletContextResource
ServletContextResource
是为了方便你从ServletContext
中获取资源而设计的,可以从相对于web
应用程序的根目录中获取资源。
InputStreamResource
InputStreamResource
是用来从给定的InputStream
中获取资源的。
ByteArrayResource
ByteArrayResource
用来从给定的字节数组中获取资源,它会创建一个ByteArrayInputStream
。
4 ResourceLoader
ResourceLoader
接口是用来加载资源的,它提供了一个getResource
函数用来返回一个Resource
对象。下面是它的定义:
public interface ResourceLoader {
String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
Resource getResource(String location);
@Nullable
ClassLoader getClassLoader();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
所有的应用程序上下文都实现了ResourceLoader
接口,因此可以从应用程序上下文中获取Resource
对象:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
- 1
如果在调用getResource()
的时候,指定的资源路径上没有给出前缀,那么Spring
会根据context
的类型返回一个合适类型的资源对象。例如,在ClassPathXmlApplicationContext
对象上调用getResource()
函数,则会返回一个ClassPathResource
对象。
如果你指定了前缀,那么不管context
是什么类型,都将返回前缀对应的资源类型,例如:
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
- 1
下表总结了资源路径对应的资源类型:
前缀 | 例子 | 说明 |
---|---|---|
classpath: | classpath:com/myapp/config.xml |
从classpath 加载资源。 |
file: | file:///data/config.xml |
从文件系统加载资源。 |
http: | http://myserver/logo.png |
使用http 协议加载资源。 |
(none) | /data/config.xml |
根据context 类型判断。 |
5 ResourceLoaderAware接口
ResourceLoaderAware
是Spring
提供的一个回调接口,用于注入ResourceLoader
:
public interface ResourceLoaderAware extends Aware {
void setResourceLoader(ResourceLoader resourceLoader);
}
- 1
- 2
- 3
- 4
如果一个类实现了ResourceLoaderAware
接口并在Spring
上下文中注册为一个bean
,那么context
会调用它的setResourceLoader()
方法将context
本身设置进去(因为所有的context
都实现了ResourceLoader
接口)。
下面是一个例子:
@Component
public class ResourceBean implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("test/resourceLoaderAware.xml");
ResourceBean resourceBean = ctx.getBean(ResourceBean.class);
ResourceLoader loader = resourceBean.getResourceLoader();
Assert.assertTrue(loader instanceof ApplicationContext);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
上述方法采用实现
ResourceLoaderAware
接口的方式注入ResourceLoader
,属于侵入式的方法。从Spring 2.5
之后,可以直接通过@Autowired
自动注入的方式注入ResourceLoader
。
6 应用个上下文和资源路径
6.1 构造一个应用上下文
应用上下文的构造函数通常将资源的路径以一个字符串或者字符串数组传入。当路径没有前缀的时候,资源的类型依据上下文的类型构建。
下面的例子,通过资源的路径构造一个ClassPathXmlApplicationContext
上下文对象:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
- 1
下面的例子,根据资源路径构造一个FileSystemXmlApplicationContext
上下文对象:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
- 1
也可以通过路径前缀指定具体资源的类型。
6.2 资源路径支持通配符
可以通过*
来表示一批资源文件:
/WEB-INF/*-context.xml
com/mycompany/**/applicationContext.xml
file:C:/some/path/*-context.xml
classpath:com/mycompany/**/applicationContext.xml
- 1
- 2
- 3
- 4
6.3 classpath*:和classpath:
classpath*:
和classpath:
的主要区别体现在:
classpath:
:只会在当前项目的WEB-INF/classes
路径下查找文件。classpath*:
:不只在当前羡慕的WEB-INF/classes
路径下查找文件,还会到第三方jar
文件的WEB-INF/classes
查找文件。
6.4 FileSystemResource的路径问题
为了兼容,FileSystemXmlApplicationContext
中的相对路径和绝对路径都将视为相对路径,相对于当前项目的工作目录。
下面两个是等价的:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml");
- 1
ApplicationContext ctx = new FileSystemXmlApplicationContext("/conf/context.xml");
- 1
如果真的需要绝对路径,应该尽量使用UrlResource
并通过file:
前缀来指定:
// actual context type doesn't matter, the Resource will always be UrlResource
ctx.getResource("file:///some/resource/path/myTemplate.txt");
- 1
- 2
// force this FileSystemXmlApplicationContext to load its definition via a UrlResource
ApplicationContext ctx = new FileSystemXmlApplicationContext("file:///conf/context.xml");
Resource接口的更多相关文章
- 攻城狮在路上(贰) Spring(三)--- Spring 资源访问利器Resource接口
Spring为了更好的满足各种底层资源的访问需求.设计了一个Resource接口,提供了更强的访问底层资源的能力.Spring框架使用Resource装载各种资源,包括配置文件资源.国际化属性文件资源 ...
- Resource接口,及资源
Resource介绍 编码的时候,除了代码本身,我们还需要对外部的资源进行处理.例如:URL资源.URI资源.File资源.ClassPath相关资源.服务器相关资源(VFS等)等等. 而这些资源的处 ...
- spring-第十四篇之资源访问Resource接口
1.Resource接口提供的主要方法 1>getInputStream():定位并打开资源,返回资源对应的输入流.每次调用都返回新的输入流.调用者必须负责关闭输入流. 2>isOpen( ...
- 通过Spring Resource接口获取资源
目录 1 Resource简介 2 通过ResourceLoader获取资源 3 在bean中获取Resource的方式 1 Resource简介 在S ...
- Spring系列.Resource接口
接口简介 JDK中提供了java.net.URL这个类来用于获取不同种类的资源(根据不同前缀的url可以获取不同种类的资源).但是URL这个类没有获取classpath和ServletContext下 ...
- Spring系列18:Resource接口及内置实现
本文内容 Resource接口的定义 Resource接口的内置实现 ResourceLoader接口 ResourceLoaderAware 接口 Resource接口的定义 Java 的标准 ja ...
- Spring Resource接口获取资源
1.1.1. Resource简介 在Spring内部实现机制,针对于资源文件(配置的xml文件)有一个统一的接口Resource. 1.1.1.1. 接口定义的方法 1.exists():判断资源文 ...
- Spring中Resource接口的前缀书写格式
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt"); //这个 ...
- Bean利用Resource接口获取资源的几种方式
Resources的类型 获取resource的方式(xml配置正常进行):
随机推荐
- HTML中设置在浏览器中固定位置fixed定位
之前的博文 HTML布局排版之制作个人网站的文章列表,中链接到的文章本身,也需要返回到列表主页,可在每个文章页面加导航条,也可以只加个返回到列表主页的链接.刚开始是想在博文最下方,加个返回文章列表的链 ...
- 07点睛Spring4.1-BeanPostProcessor
7.1 BeanPostProcessor spring通过BeanPostProcessor接口可以对所有bean或者指定的某些bean的初始化前后对bean的检查或者修改提供支持; 使用postP ...
- R镜像源的切换
如果是默认的R安装一般会很慢 install.packages(pkgs, lib, repos = getOption("repos"), contriburl = contri ...
- webpack package code into different bundle
Demo4操作手册 本Demo演示如何进行分块打包等较高级的使用 准备环境 初始化环境, cd到demo1目录之后, 执行如下命令: npm init -y npm install webpack w ...
- Java基础笔试练习(一)
1. 若在某一个类定义中定义有如下的方法: abstract void performDial( ); 该方法属于() ? A.本地方法 B.最终方法 C.静态方法 D.抽象方法 答案: D 解析: ...
- Python之匿名函数使用示例
#!/usr/bin/env python # -*- coding:utf8 -*- # #匿名函数 # y = lambda x:x+1 # print(y(10)) name = 'AK' #一 ...
- SDK的使用步骤
SDK包括三种类型文件: (1).头文件(.h) (2).库文件(.lib) (3).动态库(.dll) 第一步:在项目目录中新建一个Libs文件夹,再在该文件夹中分别新建inc文件夹和lib文件夹, ...
- memset 导致的一个段错误
原型: void *memset(void *s, int c, size_t n); 解释: memset :是 逐字节 拷贝,即n是指整个变量所占字节,在用于数组时一定要注意n不一定是 数组元素. ...
- 十九、eMMC驱动框架分析
一.MMC简介 eMMC在封装中集成了一个控制器,提供标准接口并管理Nand Flash,使得手机厂商就能专注于产品开发的其它部分,并缩短向市场推出产品的时间. 对于我们来说,eMMC就是在Nand ...
- xorm表结构操作实例
获取数据库信息 package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "git ...