spring-core io包ResourceLoader接口源码解析

 一、接口类继承图:

二、接口功能概述和实现    

ResourceLoader接口定义了获取资源的接口,如下图:

 DefaultResourceLoader提供了默认实现,默认实现中通过ClassLoader读取类根路径下的文件资源,也可以读取URL形式的文件资源,子类可以通过addProtocolResolver(ProtocolResolver resolver)或者覆写getResourceByPath(String path)方法来改变默认的读取方式。ProtocolResolver的接口很简单,只有一个方法Resource resolve(String location, ResourceLoader resourceLoader)。

ClassRelativeResourceLoader继承自DefaultResourceLoader,覆写了getResourceByPath(String path)方法,将通过ClassLoader读取类根路径下的文件资源改成通过Class读取。

FileSystemResourceLoader继承自DefaultResourceLoader,覆写了getResourceByPath(String path)方法,将通过ClassLoader读取类根路径下的文件资源改成通过FileSystemResource类读取。
ResourceEditor类综合了PropertyResolver实现类和ResourceLoader实现类,默认是StandardEnvironment类和DefaultResourceLoader类读取某个资源,通过PropertyResolver解析路径中包含的系统属性或者环境变量,通过ResourceLoader加载解析后的路径。具体使用如下:

@Test
	public void testSystemPropertyReplacement() throws Exception {
		PropertyEditor editor = new ResourceEditor();
		System.setProperty("test.prop", "test.txt");
		try {
			editor.setAsText("${test.prop}");
			Resource resolved = (Resource) editor.getValue();
			assertEquals("test.txt", resolved.getFilename());
			assertEquals(4l, resolved.contentLength());
		}
		finally {
			System.getProperties().remove("test.prop");
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值