Spring源码解析三

上一节,我们了解到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的。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring源码解析是指对Spring框架的源代码进行深入分析和解读的过程。Spring框架是一个开源的Java企业级应用程序框架,提供了高度灵活和可扩展的开发环境。 Spring框架的源代码解析涉及了众多的模块和组件,包括核心容器、AOP(面向切面编程)、数据访问、Web开发等。通过对这些模块和组件的源代码进行解析,我们可以更加深入地了解Spring框架的工作原理和设计思想。 Spring源码解析的好处在于,可以帮助我们更好地理解Spring框架的各种功能和特性,并且能够帮助开发人员更加高效地使用和定制Spring框架,解决实际项目开发中的问题。 在进行Spring源码解析时,我们可以关注一些关键的概念和类,比如BeanFactory、ApplicationContext、BeanPostProcessor、AOP代理等。这些核心类和概念是理解Spring框架工作机制的重要基础。 进行Spring源码解析时,我们可以使用一些常见的工具和方法,比如IDE(集成开发环境)的调试功能、查看和分析源代码的注释和文档、调试和运行项目的示例代码等。 通过Spring源码解析,我们可以学到很多有关软件开发的知识和经验,比如面向对象编程、设计模式、依赖注入、控制反转等。这些知识和经验对于我们提升自己的技术水平和解决实际项目中的问题都有很大的帮助。 总之,Spring源码解析是一项非常有价值的学习和研究工作,可以帮助我们更好地理解和应用Spring框架,提高自己的技术能力和软件开发水平。希望以上的回答能够满足您的需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youngerone123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值