上一节,我们了解到Spring的基础容器XmlBeanFactory是如果使用的,并且分析了Spring容器的基本原理。那我们要怎样进行Spring的深入分析呢?Spring源码是一个很复杂的框架,因此我们抓住一条主干线进行分析,我们需要从ApplicationContext.xml开始入手。ApplicationContext.xml是通过ClassPathResource这个类加载的。接下来我们需要深入的了解ClassPathResource这个类到底是什么?我们先看一下ClassPathResource这个类的继承关系图:
可以看到,ClassPathResource其实就是一个实现Resource接口的实现类而已,和ClassPathResource类似的还有UrlResource、InputStreamResource、ByteArrayResource、FileSystemResource,它们都实现了Resource接口,而Resource接口有实现了InputStreamResource接口。
那Resource和InputStreamResource接口又是什么呢?各种Resource实现类之间的区别又是什么呢?
我们先看Resource这个接口,Spring统一把所有使用到的资源都抽象成了Resource,不同来源的资源对应不同Resource的实现类。
public interface Resource extends InputStreamSource {
/**
* 判断资源是否存在
*/
boolean exists();
// 资源是否可读
default boolean isReadable() {
return exists();
}
// 资源是否打开
default boolean isOpen() {
return false;
}
/**
* 判断资源是否是文件类型
*/
default boolean isFile() {
return false;
}
// 资源转换为RUL
URL getURL() throws IOException;
// 资源转换为 URI
URI getURI() throws IOException;
//资源转换为文件
File getFile() throws IOException;
/**
* Return a {@link ReadableByteChannel}.
* <p>It is expected that each call creates a <i>fresh</i> channel.
* <p>The default implementation returns {@link Channels#newChannel(InputStream)}
* with the result of {@link #getInputStream()}.
* @return the byte channel for the underlying resource (must not be {@code null})
* @throws java.io.FileNotFoundException if the underlying resource doesn't exist
* @throws IOException if the content channel could not be opened
* @since 5.0
* @see #getInputStream()
*/
default ReadableByteChannel readableChannel() throws IOException {
return Channels.newChannel(getInputStream());
}
// 资源的长度
long contentLength() throws IOException;
/**
* Determine the last-modified timestamp for this resource.
* @throws IOException if the resource cannot be resolved
* (in the file system or as some other known physical resource type)
*/
long lastModified() throws IOException;
/**
* Create a resource relative to this resource.
* @param relativePath the relative path (relative to this resource)
* @return the resource handle for the relative resource
* @throws IOException if the relative resource cannot be determined
*/
Resource createRelative(String relativePath) throws IOException;
// 获取资源的名称
@Nullable
String getFilename();
// 资源描述,用于打印资源的日志信息
String getDescription();
}
InputStreamResource 接口又是啥呢?
可以看到InputStreamSource接口中只有一个方法getInputStream,而且方法返回的就是一个输入流InputStream。
从前面的类图中我们可以看到,Resource是继承了InputStreanSource 接口,所以所有的资源只要封装成了Resource,就可以通过调用InputStreamResource的getInputStream方法,获取对应的输入流Inputstream了。由于资源的又是多种对样的,比如我们applicationContext.xml,是存放在classPath路径下的xml文件,所有需要使用ClassPathResource加载classPath 路径下的资源文件。
而 UrlResource、FileSystemResource、ByteArrayResource和InputStreamResource,他们分别是加载URL、文件、Byte数组和InputStream的Resource实现类的。
ClassPathResource中获取输入流的:
FileSystemResource 实现类获取输入流:
UrlResource 实现类获取输入流:
所以,通过发现不同的实现类获取输入流的方式是不一样的。Resource 就是Spring内部对各种资源的一个抽象而已,而InputStreamrResource接口实现,使我们对各种资源都可以轻松的获取对应的输入流。向Spring这种对资源的一个类体系的设计,值得我们学习的。在我们进行类的设计也可以参考Spring的。