spring(四):Resource
Resource
Spring的Resource接口代表底层外部资源,提供了对底层外部资源的一致性访问接口。
public interface Resource extends InputStreamSource {
boolean exists();
default boolean isReadable() { return this.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(this.getInputStream());
}
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String var1) throws IOException;
@Nullable
String getFilename();
String getDescription();
}
Resource接口提供了足够的抽象,足够满足我们日常使用。而且提供了很多内置Resource实现:ByteArrayResource、InputStreamResource 、FileSystemResource 、UrlResource 、ClassPathResource、ServletContextResource、VfsResource等。
ByteArrayResource
private final byte[] byteArray;
private final String description;
return new ByteArrayInputStream(this.byteArray);
InputStreamResource
private final InputStream inputStream;
private final String description;
private boolean read;
FileSystemResource
private final String path;
@Nullable
private final File file;
private final Path filePath;
return Files.newInputStream(this.filePath);
UrlResource
@Nullable
private final URI uri;
private final URL url;
private final URL cleanedUrl;
URLConnection con = this.url.openConnection();
return con.getInputStream();
ClassPathResource
private final String path;
@Nullable
private ClassLoader classLoader;
@Nullable
private Class<?> clazz;
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
} else if (this.classLoader != null) {
is = this.classLoader.getResourceAsStream(this.path);
} else {
is = ClassLoader.getSystemResourceAsStream(this.path);
}
ServletContextResource
代表web应用资源
private final ServletContext servletContext;
private final String path;
InputStream is = this.servletContext.getResourceAsStream(this.path);
VfsResource
代表虚拟文件系统资源
private final Object resource;
return VfsUtils.getInputStream(this.resource);
ResourceLoader接口
public interface ResourceLoader {
String CLASSPATH_URL_PREFIX = "classpath:";
Resource getResource(String var1);
@Nullable
ClassLoader getClassLoader();
}
对于目前所有ApplicationContext都实现了ResourceLoader,因此可以使用其来加载资源。
ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
// 第一类构造器是根据提供的配置文件路径使用“ResourcePatternResolver”
// 的“getResources()”接口通过匹配获取资源;即如“classpath:config.xml”
public ClassPathXmlApplicationContext(String configLocation)...
public ClassPathXmlApplicationContext(String... configLocations)...
public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent)...
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh)...
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)...
// 第二类构造器是根据提供的路径和clazz来构造ClassResource资源。
// 即采用“public ClassPathResource(String path, Class<?> clazz)”构造器获取资源。
public ClassPathXmlApplicationContext(String path, Class<?> clazz)...
public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz)...
public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent)...
FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
// 将加载相对于当前工作目录的“configLocation”位置的资源
// 在linux系统上不管“configLocation”是否带“/”,都作为相对路径
// 在window系统上如“D:/resourceInject.xml”是绝对路径。
// 因此在除非很必要的情况下,不建议使用该ApplicationContext。
public FileSystemXmlApplicationContext(String configLocation)...
public FileSystemXmlApplicationContext(String... configLocations)...
public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent)...
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh)...
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)...
WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;
其他:不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。
spring(四):Resource的更多相关文章
- Spring注解 @Resource和@Autowired
@Resource和@Autowired两者都是做bean的注入使用.其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spri ...
- Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象
-------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...
- Spring Security Resource Server的使用
Spring Security Resource Server的使用 一.背景 二.需求 三.分析 四.资源服务器认证流程 五.实现资源服务器 1.引入jar包 2.资源服务器配置 3.资源 六.测试 ...
- Spring注解@Resource和@Autowired区别对比
转载:http://www.cnblogs.com/think-in-java/p/5474740.html @Resource和@Autowired都是做bean的注入时使用,其实@Resource ...
- Spring 注解 @Resource和@Autowired(转)
鸣谢:http://my.oschina.net/u/216467/blog/205951 @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spri ...
- Spring 注解 @Resource和@Autowired
@Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...
- Spring注解@Resource和@Autowired的区别
@Resource和@Autowired都是用来做bean的依赖注入的,两者都可以写在字段和setter方法上. java为我们提供了 javax.annotation.Resource这个注解. s ...
- 【转载】Spring注解@Resource和@Autowired区别对比
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...
- 【Java】Spring之Resource(三)
Java的各种URL前缀的标准类和标准处理程序不足以完全访问低级资源.例如,没有URL可用于访问需要从类路径或相对于a获取的资源的标准化实现 ServletContext.虽然可以为专用URL 前缀注 ...
- spring四种依赖注入方式(转)
spring四种依赖注入方式!! 平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提 ...
随机推荐
- oracke数据库分区新增
1.发现一些过程执行报错,报错原因是'ora-14400插入的分区关键字未映射到任何分区',原来是2020年库表的时间子分区未扩展: 2.扩展子分区的语句是 ALTER TABLE 表名 MODIFY ...
- java如何在静态方法中访问类的实例成员
类的静态方法是不能直接访问实例的成员的,它只能访问同类的静态成员.访问实例的成员的话,可以参考一下这个方法,那就是把静态方法的参数设置为类的实例,这样通过参数传递的方式就可以访问实例的成员了,例子如下 ...
- Codeforces Round #617 (Div. 3) 补题记录
1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...
- 1级搭建类110-Oracle 18c SI FS(Windows Server 2019)公开
Oracle 18c 单实例文件系统在Windows Server 2019上的安装 在线查看
- 二叉堆(3)SkewHeap
斜堆. 测试文件 main.cpp: #include <iostream> #include "SkewHeap.h" using std::cout; using ...
- LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...
- kuangbin专题专题十一 网络流 Minimum Cost POJ - 2516
题目链接:https://vjudge.net/problem/POJ-2516 思路:对于每种商品跑最小费用最大流,如果所有商品和人一起建图跑,O(v^2*m)数量级太大,会超时. 把店里的商品拆点 ...
- 记录 Docker 的学习过程 (数据挂载)
docker 存储篇 容器中的存储是分层的, 在容器中,如果我们要创建一个文件,会在文件的最上层(可写层)创建 容器中内置的文件,默认来讲是只读的,只有自己创建的文件才是可写状态 比如说 /etc/p ...
- [P3935] Calculating - 整除分块
容易发现题目要求的 \(f(x)\) 就是 \(x\) 的不同因子个数 现在考虑如何求 \(\sum_{i=1}^n f(i)\),可以考虑去算每个数作为因子出现了多少次,很容易发现是 \([n/i] ...
- new Vue发生了什么(五)
从入口代码开始分析,我们先来分析 new Vue 背后发生了哪些事情.我们都知道,new 关键字在 Javascript 语言中代表实例化是一个对象,而 Vue 实际上是一个类,类在 Javascri ...