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的更多相关文章

  1. Spring注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用.其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spri ...

  2. Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象

    -------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...

  3. Spring Security Resource Server的使用

    Spring Security Resource Server的使用 一.背景 二.需求 三.分析 四.资源服务器认证流程 五.实现资源服务器 1.引入jar包 2.资源服务器配置 3.资源 六.测试 ...

  4. Spring注解@Resource和@Autowired区别对比

    转载:http://www.cnblogs.com/think-in-java/p/5474740.html @Resource和@Autowired都是做bean的注入时使用,其实@Resource ...

  5. Spring 注解 @Resource和@Autowired(转)

    鸣谢:http://my.oschina.net/u/216467/blog/205951 @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spri ...

  6. Spring 注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...

  7. Spring注解@Resource和@Autowired的区别

    @Resource和@Autowired都是用来做bean的依赖注入的,两者都可以写在字段和setter方法上. java为我们提供了 javax.annotation.Resource这个注解. s ...

  8. 【转载】Spring注解@Resource和@Autowired区别对比

    @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...

  9. 【Java】Spring之Resource(三)

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

  10. spring四种依赖注入方式(转)

    spring四种依赖注入方式!! 平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提 ...

随机推荐

  1. JS中BOM操作知识点

    JS BOM window对象 全局变量和全局方法都归在window上 alert-comfirm-prompt 让alert .confirm等弹出框上的提示文字实现换行:\n // confirm ...

  2. ng-简介

    Angular 是什么 Angular(读音['æŋgjʊlə])是一套用于构建用户界面的 JavaScript 框架.由 Google 开发和维护,主要被用来开发单页面应用程序. 类似于 Vue.j ...

  3. Html5学习系列

    Html5学习系列 HTML5 规定了一种通过 video 元素来包含视频的标准方法 Ogg = 带有 Theora 视频编码和 Vorbis 音频编码的 Ogg 文件 MPEG4 = 带有 H.26 ...

  4. Centos 安装SVN

    Centos 安装SVN  centos(我这里使用的是CentOS7)下yum命令即可方便的完成安装 $ sudo yum install subversion 测试安装是否成功: $ svnser ...

  5. WebMethods开发入门

    webMethods  Integration Platform 由用于设计.执行和管理集成解决方案的 3 类组件构成. 设计时组件:这些组件提供了开发和测试集成解决方案的工具. 1.webMetho ...

  6. 0级搭建类007-Ubuntu Desktop Linux安装 (18.04.2) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  7. centos7上python3.6.5的安装及卸载

    前言 最近开始安装配置公司给我的台式机,加上刚刚购买的ECS,处女座的我,环境前前后后大概装了有十来次吧,之前装总是临时网上找教程,但是装下来总是感觉有点别扭,当时服务器装的是3.6.5,虚拟机装的是 ...

  8. 你写的 Python 代码总是不规范?用它!

    今天咱们来说说 代码风格 ! 不同的编程语言 有不同的代码风格 ​ ​ ​ ​ ​ Python 的代码规范 就是人们常说的 PEP8 ​ ​ 在这个网站 https://www.python.org ...

  9. 2_abstractions

    2. Up and down the level of abstraction In this chapter, we'll travel up and down the level of abstr ...

  10. ansible笔记(14):循环(一)

    在使用ansible的过程中,我们经常需要处理一些返回信息,而这些返回信息中,通常可能不是单独的一条返回信息,而是一个信息列表,如果我们想要循环的处理信息列表中的每一条信息,我们该怎么办呢?这样空口白 ...