ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring系列.Resource接口

2021-02-27 22:03:51  阅读:161  来源: 互联网

标签:ApplicationContext Resource Spring ctx 接口 resource 资源


接口简介

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

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;
	}//加入Java开发交流君样:756584822一起吹水聊天
	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 {
//加入Java开发交流君样:756584822一起吹水聊天
    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);
}//加入Java开发交流君样:756584822一起吹水聊天

所有的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");
//加入Java开发交流君样:756584822一起吹水聊天
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 
//加入Java开发交流君样:756584822一起吹水聊天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”);

image

最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:756584822

标签:ApplicationContext,Resource,Spring,ctx,接口,resource,资源
来源: https://blog.csdn.net/weixin_52472392/article/details/114194048

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有