[源码系列:手写spring] IOC第六节:资源和资源加载器
主要内容

本节新增
Resource接口 定义对资源的抽象和访问,并且添加三个Resource接口的简单实现类。
FileSystemResource 文件系统资源的实现类
ClassPathResource classpath下资源的实现类
UrlResource 对java.net.URL进行资源定位的实现类
ResourceLoader接口 资源加载器接口
DefaultResourceLoader 加载器实现类
代码分支
核心代码
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第六节:资源和资源加载器的更多相关文章
- Spring源码分析 手写简单IOC容器
Spring的两大特性就是IOC和AOP. IOC Container,控制反转容器,通过读取配置文件或注解,将对象封装成Bean存入IOC容器待用,程序需要时再从容器中取,实现控制权由程序员向程序的 ...
- 《四 spring源码》手写springioc框架
手写SpringIOCXML版本 /** * 手写Spring专题 XML方式注入bean * * * */ public class ClassPathXmlApplicationContext { ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- 从零开始手写 spring ioc 框架,深入学习 spring 源码
IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...
- 《四 spring源码》手写springmvc
手写SpringMVC思路 1.web.xml加载 为了读取web.xml中的配置,我们用到ServletConfig这个类,它代表当前Servlet在web.xml中的配置信息.通过web.xml ...
- Spring源码 20 手写模拟源码
参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...
- 框架源码系列十二:Mybatis源码之手写Mybatis
一.需求分析 1.Mybatis是什么? 一个半自动化的orm框架(Object Relation Mapping). 2.Mybatis完成什么工作? 在面向对象编程中,我们操作的都是对象,Myba ...
- 利用递归,反射,注解等,手写Spring Ioc和Di 底层(分分钟喷倒面试官)了解一下
再我们现在项目中Spring框架是目前各大公司必不可少的技术,而大家都知道去怎么使用Spring ,但是有很多人都不知道SpringIoc底层是如何工作的,而一个开发人员知道他的源码,底层工作原理,对 ...
- Spring源码剖析3:Spring IOC容器的加载过程
本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...
- 源码分析 | 手写mybait-spring核心功能(干货好文一次学会工厂bean、类代理、bean注册的使用)
作者:小傅哥 博客:https://bugstack.cn - 汇总系列原创专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 一个知识点的学习过程基本分为:运行helloworld ...
随机推荐
- 详解AQS五:深入理解共享锁CountDownLatch
CountDownLatch是一个常用的共享锁,其功能相当于一个多线程环境下的倒数门闩.CountDownLatch可以指定一个计数值,在并发环境下由线程进行减一操作,当计数值变为0之后,被await ...
- Solution Set - “伸手向着拉格朗日点作别”
目录 0.「UR #9」「UOJ #133」电路手动分析 1.「UR #9」「UOJ #134」App 管理器 2.「UR #10」「UOJ #152」汉诺塔 3.「UNR #2」「UOJ #312」 ...
- js操作shadow-root内的DOM元素
其实就是 documentfragment元素,就是动态生成的文档碎片元素. 1,项目中在DOM结构里遇到了shadow-root(open),用JS方法无法直接获取其内的DOM元素 2.shadow ...
- 微服务实战系列(二)-注册中心Springcloud Eureka客户端-copy
1. 场景描述 前几天介绍了下springcloud的Eureka注册中心(springcloud-注册中心快速构建),今天结合springboot-web介绍下eureka客户端服务注册. 2. 解 ...
- Golang-接口7
http://c.biancheng.net/golang/interface/ Go语言接口声明(定义) Go语言不是一种 "传统" 的面向对象编程语言:它里面没有类和继承的概念 ...
- 注册全局组件(H5) 任意页面使用
在view下创建components文件夹. 在components下创建文件夹base. base文件夹是用来存放 基础组件的. 比如说页面中很多处都在使用的公共组件 如你需要自定义的按钮 在com ...
- 文本处理命令head tail more less tr cut paste wc
文本处理命令 命令**head tail more less tr cut paste wc** 磁盘分区利用率 df|tr -s ' ' :|cut -d : -f5 df|tr -s ' ' :| ...
- Mac安装Zookeeper
软件版本:3.4.10 一.软件下载 下载链接:http://archive.apache.org/dist/zookeeper/ 解压缩:tar -zxf zookeeper-3.4.10.t ...
- nginx 简单实践:Web 缓存【nginx 实践系列之三】
〇.前言 本文为 nginx 简单实践系列文章之二,主要简单实践了两个内容:正向代理.反向代理,仅供参考. 关于 Nginx 基础,以及安装和配置详解,可以参考博主过往文章: https://www. ...
- hbase - [04] java访问hbase
需要导入jar包 $HBASE_HOME/lib下的所有jar包 $HADOOP_HOME/share/hadoop/common的所有jar包 package com.harley.hbase.te ...