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提 ...
随机推荐
- 小白的java学习之路 “ 选择结构(一)”
if选择结构: if选择结构是根据条件判断之后再做处理的一种语法结构. 1.if选择结构的语法: public class Demo{ public static void main(String[] ...
- 解决officeOnline文档预览服务器只能域名提交的限制Redirect
此项目是解决officeOnline文档预览只能用域名提交的限制 http://officeOnline文档预览域名或IP/op/generate.aspx // 微软原生页面 创建链接后会生成全屏预 ...
- Android电源管理基础知识整理
前言 待机.睡眠与休眠的区别? Android开发者官网当中提到"idle states",该如何理解,这个状态会对设备及我们的程序造成何种影响? 进入Doze模式中的idle状态 ...
- 史上最深入浅出的IT术语解读
假设你是个妹子,你有一位男朋友,与此同时你和另外一位男生暧昧不清,比朋友好,又不是恋人.你随时可以甩了现任男友,另外一位马上就能补上.这是冷备份. 假设你是个妹子,同时和两位男性在交往,两位都是你男朋 ...
- Beego :布局页面
1:页面布局 一个html页面由:head部分,body部分,内部css,内部js,外联css,外联的js这几部分组成.因此,一个布局文件也就需要针对这些进行拆分. 2> 新建一个lay ...
- IO流学习之字符流(三)
IO流之字符流缓冲区: 概念: 流中的缓冲区:是先把程序需要操作的数据保存在内存中,然后我们的程序读写数据的时候,不直接和持久设备之间交互,而改成和内存中的数据进行交互. 缓冲区:它就是临时存储数据, ...
- PIE-SDK For C++栅格数据的创建
1.功能简介 目前在地理信息领域中数据包括矢量和栅格两种数据组织形式.每一种数据有不同的数据格式,目前PIE SDK支持多种数据格式的数据创建,下面对栅格数据格式的数据创建功能进行介绍. 2.功能实现 ...
- Linux命令——细节
echo -n 不换行输出 echo -e 处理特殊字符 若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出: \a 发出警告声: \b 删除前一个字符: \c 最后不加上换行符号: ...
- Vue中常见参数传递方式
文章内容:这里只有vue中父子组件传参.路由间的传参 (另外还有vuex.储存本地.中央bus等方式) 一.父子组件 1.1父传子(props) <!-- 父组件father.vue --> ...
- 机器学习作业(五)机器学习算法的选择与优化——Matlab实现
题目下载[传送门] 第1步:读取数据文件,并可视化: % Load from ex5data1: % You will have X, y, Xval, yval, Xtest, ytest in y ...