主要内容

本节新增

  • Resource接口 定义对资源的抽象和访问,并且添加三个Resource接口的简单实现类。

  • FileSystemResource 文件系统资源的实现类

  • ClassPathResource classpath下资源的实现类

  • UrlResource 对java.net.URL进行资源定位的实现类

  • ResourceLoader接口 资源加载器接口

  • DefaultResourceLoader 加载器实现类

代码分支

resource-and-resource-loader

核心代码

Resource

public interface Resource {
InputStream getInputStream() throws IOException;
}

ResourceLoader

public interface ResourceLoader {
Resource getResource(String location);
}

ClassPathResource

public class ClassPathResource implements Resource{
private final String path; public ClassPathResource(String path) {
this.path = path;
} @Override public InputStream getInputStream() throws FileNotFoundException {
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(path);
if (stream == null) {
throw new FileNotFoundException(this.path + " cannot be opened because it does not exist");
}
return stream; }
}

FileSystemResource

public class FileSystemResource implements Resource{
private final String filePath; public FileSystemResource(String filePath) {
this.filePath = filePath;
} @Override public InputStream getInputStream() throws IOException{
Path path = new File(this.filePath).toPath();
return Files.newInputStream(path);
}
}

UrlResource

public class UrlResource implements Resource{
private final URL url; public UrlResource(URL url) {
this.url = url;
} @Override public InputStream getInputStream() throws IOException {
URLConnection urlConnection = this.url.openConnection();
return urlConnection.getInputStream();
}
}

DefaultResourceLoader

public class DefaultResourceLoader implements ResourceLoader{
public static final String CLASSPATH_URL_PREFIX = "classpath:";
@Override public Resource getResource(String location) {
if(location.startsWith(CLASSPATH_URL_PREFIX)){
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
}else {
try {
URL url = new URL(location);
return new UrlResource(url);
}catch (Exception e){
return new FileSystemResource(location);
} }
}
}

测试

@Test
public void test() throws IOException {
DefaultResourceLoader loader = new DefaultResourceLoader();
//classpath下资源的加载
Resource resource = loader.getResource("classpath:test.txt");
InputStream inputStream = resource.getInputStream();
String content = IoUtil.readUtf8(inputStream);
System.out.println("classpath - 资源内容测试:"+content); //文件系统资源的加载
resource = loader.getResource("src/test/resources/test.txt");
inputStream = resource.getInputStream();
content = IoUtil.readUtf8(inputStream);
System.out.println("文件系统 - 资源内容测试:"+content);
//url资源的加载
resource = loader.getResource("https://blog.csdn.net/weixin_43848166");
inputStream = resource.getInputStream();
content = IoUtil.readUtf8(inputStream);
System.out.println("URL - 资源内容测试:"+content);
}

测试结果

classpath - 资源内容测试:hello-word
文件系统 - 资源内容测试:hello-word
URL - 资源内容测试:<!doctype html><html lang="zh" data-server-rendered="true" data-v-52866abc><head><title>一辉ComeOn的博客_CSDN博客-Spring源码剖析,每日算法,MySQL进阶领域博主</title>
...

[源码系列:手写spring] IOC第六节:资源和资源加载器的更多相关文章

  1. Spring源码分析 手写简单IOC容器

    Spring的两大特性就是IOC和AOP. IOC Container,控制反转容器,通过读取配置文件或注解,将对象封装成Bean存入IOC容器待用,程序需要时再从容器中取,实现控制权由程序员向程序的 ...

  2. 《四 spring源码》手写springioc框架

    手写SpringIOCXML版本 /** * 手写Spring专题 XML方式注入bean * * * */ public class ClassPathXmlApplicationContext { ...

  3. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

  4. 从零开始手写 spring ioc 框架,深入学习 spring 源码

    IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...

  5. 《四 spring源码》手写springmvc

    手写SpringMVC思路 1.web.xml加载  为了读取web.xml中的配置,我们用到ServletConfig这个类,它代表当前Servlet在web.xml中的配置信息.通过web.xml ...

  6. Spring源码 20 手写模拟源码

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  7. 框架源码系列十二:Mybatis源码之手写Mybatis

    一.需求分析 1.Mybatis是什么? 一个半自动化的orm框架(Object Relation Mapping). 2.Mybatis完成什么工作? 在面向对象编程中,我们操作的都是对象,Myba ...

  8. 利用递归,反射,注解等,手写Spring Ioc和Di 底层(分分钟喷倒面试官)了解一下

    再我们现在项目中Spring框架是目前各大公司必不可少的技术,而大家都知道去怎么使用Spring ,但是有很多人都不知道SpringIoc底层是如何工作的,而一个开发人员知道他的源码,底层工作原理,对 ...

  9. Spring源码剖析3:Spring IOC容器的加载过程

    本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...

  10. 源码分析 | 手写mybait-spring核心功能(干货好文一次学会工厂bean、类代理、bean注册的使用)

    作者:小傅哥 博客:https://bugstack.cn - 汇总系列原创专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 一个知识点的学习过程基本分为:运行helloworld ...

随机推荐

  1. kubernetes系列(八) - 控制器的资源清单定义

    1. ReplicaSet 1.1 ReplicaSet资源清单 1.2 selector 2. Deployment 2.1 Deployment资源清单 2.2 其他相关操作 2.2.1 应用ya ...

  2. Appium_WebDriverAgent安装

      一.WebDriverAgent安装到ios测试设备 a) 切换到appium 的appium-webdriveragent目录(/Applications/Appium.app/Contents ...

  3. ThreadPoolExecutor详解及线程池优化

    前言ThreadPoolExecutor在concurrent包下,是我们最常用的类之一.无论是做大数据的,还是写业务开发,对其透彻的理解以及如何发挥更好的性能,成为了我们在更好的coding道路上必 ...

  4. lxl 讲课的记录

    D1 lxl:LCT 没有前途.所以平衡树一般只需要 fhq-treap. 线段树.平衡树简单例题 P3215 注意到抵消掉合法括号串之后一定是这样的情况:))))((((即前缀最小值 \(a\).后 ...

  5. Atcoder ABC342D Square Pair 题解 [ 绿 ] [ 数论 ] [ 唯一分解定理 ]

    Square Pair:唯一分解定理好题. 引理 若 \(x=y^2\times z\),且 \(x\) 是完全平方数,那么 \(z\) 也一定是完全平方数. 证明可以用唯一分解定理证,每次把偶数次幂 ...

  6. jconsole配置

    使用jconsole远程监控可执行jar(非Tomcat)运行状况的配置 程序启动参数配置: nohup /data/soft/jdk1.8.0_251/bin/java -Dcom.sun.mana ...

  7. 《Indie Tools • 半月刊》第001期

    引言:独立开发者工具分享 <INDIE TOOLS>专注于分享独立开发出海精选.最新.最实用的工具. 欢迎订阅半月刊:<INDIE TOOLS • 半月刊> 如果本文能给你提供 ...

  8. 程序员的生产力神器Cursor -新手实操指南

    不得不说这个Cursor AI简直是神器中的神器! 代码自动补全就算了,关键是它能直接读懂我的意图,秒懂需求!为你自动生成整个项目级别的代码!开发过程丝滑得像在跟老朋友聊天,代码质量贼高,效率提升10 ...

  9. Dev Express WPF GridControl 数据导出到Excel

    Dev Express WPF 给控件提供了公共的导出方法: Export to PDF Export to HTML Export to MHT Export to Text Export to C ...

  10. 若依-Vue 单体版本 更换mybatisPlus

    1.单体模块在pom.xml ; 多模块版本在ruoyi-common\pom.xml.模块添加整合依赖 <!-- mybatis-plus 增强CRUD --> <dependen ...