[源码系列:手写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 ...
随机推荐
- AI在线文档手册
AI在线文档手册 ABCDFGHIKLMNOPRSTW A Accord.NET 英文文档Azure ML StudioAmazon Machine LearningAmazon Rekognitio ...
- 第一章 dubbo源码解析目录
重要的网址: dubbo的github:https://github.com/alibaba/dubbo dubbo官网:http://dubbo.io/ dubbo使用者手册:https://dub ...
- 某Websocket反爬逆向分析+请求加解密+还原html
网址 aHR0cHM6Ly93d3cueWR4Zi5nb3YuY24vTmV3cy9zaG93TGlzdC80L3BhZ2VfMS5odG1s 前言 工作中遇到的某websocket反爬,比混淆网站还 ...
- 从存钱罐到子数组:一个关于累加和的精妙问题|LeetCode 560 和为K的子数组
LeetCode 560 和为K的子数组 点此看全部题解 LeetCode必刷100题:一份来自面试官的算法地图(题解持续更新中) 生活中的算法 你有没有这样的经历:每天往存钱罐里存一些零钱,某一天突 ...
- 一种Mysql和Mongodb数据同步到Elasticsearch的实现办法和系统
本文分享自天翼云开发者社区<一种Mysql和Mongodb数据同步到Elasticsearch的实现办法和系统>,作者:l****n 核心流程如下: 核心逻辑说明: MySQL Binlo ...
- 手把手教你喂养 DeepSeek 本地模型
上篇文章<手把手教你部署 DeepSeek 本地模型>首发是在公众号,但截止目前只有500多人阅读量,而在自己博客园BLOG同步更新的文章热度很高,目前已达到50000+的阅读量,流量是公 ...
- Paxos算法:如何解决分布式系统中的共识问题?
背景 Paxos 算法是 Leslie Lamport(莱斯利·兰伯特)在 1990 年提出了一种分布式系统 共识 算法.这也是第一个被证明完备的共识算法(前提是不存在拜占庭将军问题,也就是没有恶意节 ...
- 中国最难入职的IT公司排行榜
在IT行业竞争日益白热化的今天,头部企业的招聘门槛不断刷新求职者的认知.根据最新行业调研和招聘数据,我们整理出2025年中国最难入职的几家互联网公司,并揭秘其背后严苛的选拔逻辑. 通常衡量难不难,会从 ...
- Go实现动态开点线段树
1.线段树介绍 线段树是一种用于高效处理区间查询和区间更新的数据结构,当我们需要解决一个频繁更新区间值的问题的时候,就可以采用线段树的结构进行解决.线段树的核心思想是将区间分为多个子区间进行管理,越往 ...
- flutter - [02] 基本语法
题记部分 一.注释 ///这是一个注释 //这也是个注释 /* 这还是个注释 */ void main(List<String> args) { print ('你好 dart'); } ...