16、Resources
16.1、Resource接口
在Spring中有一个Resources包,我们项目创建的一些XML文件等配置文件,都是保存在这个文件内的
而用于访问这些资源的接口为 Resource接口,位于org.springframework.core.io 包中,用于抽象对低级资源的访问。
public interface Resource extends InputStreamSource {
//确定此资源是否实际以物理形式存在
boolean exists();
//表明资源的目录读取是否通过getInputStream()进行读取。
boolean isReadable();
//指示此资源是否表示具有打开流的句柄。如果为true ,则 InputStream 不能被多次读取,必须读取并关闭以避免资源泄漏。
boolean isOpen();
//确定此资源是否表示文件系统中的文件。
boolean isFile();
//返回此资源的 URL 句柄
URL getURL() throws IOException;
//返回此资源的 URI 句柄。
URI getURI() throws IOException;
//返回某个文件,如果资源不能够被解析称为绝对路径,将会抛出FileNotFoundException
File getFile() throws IOException;
//返回一个ReadableByteChannel 。
ReadableByteChannel readableChannel() throws IOException;
//确定此资源的内容长度。
long contentLength() throws IOException;
//资源最后一次修改的时间戳
long lastModified() throws IOException;
//创建此资源的相关资源
Resource createRelative(String relativePath) throws IOException;
//确定该资源的文件名——通常是路径的最后一部分——例如, "myfile.txt" 。如果此类资源没有文件名,则返回null 。
String getFilename();
//返回资源的描述,用来输出错误的日志
String getDescription();
}
Resource 接口是 Spring 资源访问策略的抽象,本身并不提供任何资源访问实现,具体由实现类完成,Resource一般包括这些实现类:UrlResource、ClassPathResource、FileSystemResource、ServletContextResource、InputStreamResource、ByteArrayResource
-
UrlResource:来访问网络资源,它支持URL的绝对路径。
- http:------该前缀用于访问基于HTTP协议的网络资源。
- ftp:------该前缀用于访问基于FTP协议的网络资源
- file: ------该前缀用于从文件系统中读取资源
-
ClassPathResource:用来访问类加载路径下的资源,比如我们很多时候都会写classpath:**.xml ,这种格式,当Spring识别该字符串参数中包含 classpath: 前缀后,系统会自动创建ClassPathResource对象。
@Test public void test(){ // 创建一个 Resource 对象 ClassPathResource resource = new ClassPathResource("aop.xml"); // 获取文件名 System.out.println("resource.getFileName = " + resource.getFilename()); // 获取文件描述 System.out.println("resource.getDescription = "+ resource.getDescription()); //获取文件内容 InputStream in = null; try { in = resource.getInputStream(); byte[] b = new byte[1024]; while(in.read(b)!=-1) { System.out.println(new String(b)); } } catch (IOException e) { throw new RuntimeException(e); } }
-
FileSystemResource:用于访问文件系统资源(类似于Java的File类),当Spring识别该字符串参数中包含file:前缀后,系统将会自动创建FileSystemResource对象。
-
ServletContextResource:ServletContext资源的Resource实现,它解释相关Web应用程序根目录中的相对路径。
-
InputStreamResource:给定的输入流(InputStream)的Resource实现。
-
ByteArrayResource:字节数组的Resource实现类。
16.2、ResourceLoader
当Spring需要进行资源访问时,并不会直接使用Resource的实现类,它在 org.springframework.core.io 包内提供了一个ResourceLoader接口,它是一个用于加载资源(例如,类路径或文件系统资源)的策略接口。
Spring进行资源访问时,调用 ResourceLoader 实例的 getResource()方法来获得资源,ReosurceLoader将会负责选择Reosurce实现类,确定具体的资源访问策略,从而将应用程序和具体的资源访问策略分离开来。(配合前缀也可以指定Resource:calsspath:、file:等)
-
ResourceLoader : 该接口实现类的实例可以获得一个Resource实例。
- Resource getResource(String location) : 用于返回一个Resource实例。ApplicationContext的实现类在最底层都实现ResourceLoader接口,因此ApplicationContext可直接获取Resource实例。
- ClassLoader getClassLoader():公开此ResourceLoader使用的ClassLoader
16.3、ResourceLoaderAware
Spring的 org.springframework.context 包内有一个 ResourceLoaderAware 接口,如果把实现ResourceLoaderAware接口的Bean部署在Spring容器中,Spring容器会将自身当成ResourceLoader作为setResourceLoader()方法的参数传入。由于ApplicationContext的实现类都实现了ResourceLoader接口,Spring容器自身完全可作为ResorceLoader使用。
- ResourceLoaderAware :该接口实现类的实例将获得一个ResourceLoader的引用。
- setResourceLoader():由Spring容器负责调用,Spring容器会将一个ResourceLoader对象作为该方法的参数传入。