撸一撸Spring Framework-IoC-资源管理利器(Resource、ResourceLoader、ResourcePatternResolver)

   撸一撸Spring Framework-IoC系列文章目录

我们经常需要读取外部资源到应用中,比如文本文件、properties文件、图片文件等。这些资源可能位于不同的位置,比如文件系统、classpath下的资源、或者远程服务器上的资源。通常,我们需要通过不同的API分别加载不同类型路径的资源,有诸多不便之处 

 Spring提供了Resource系列接口解决上述问题,API非常友好、强大

Resource:是对诸如文件系统资源、classpath资源、URL资源等各种资源的抽象

ResourceLoader:提供了统一的加载Resource的方法,它通过资源路径前缀自动匹配相应的资源类型,为开发者屏蔽了使用不同Resource实现的差异

ResourcePatternResolver:比ResourceLoader更强大,支持以通配符的方式加载所有满足条件的资源

Spring内部就通过Resource系列接口进行资源加载,比如我们定义的xml文件、properties文件、@ComponentScan扫描的java类文件等,都是通过这套接口进行加载的

除此之外,Resource相关接口可以脱离Spring独立使用,我们可以通过如下方式加载不同类型资源:

//资源加载器(ResourceLoader接口的默认实现类)
ResourceLoader resourceLoader=new DefaultResourceLoader();
//通过指定不同前缀的资源路径,加载不同类型的资源
//通过文件系统绝对路径加载资源
Resource resource = resourceLoader.getResource("file:D:/code/spring/src/main/resources/demo.xml");
//通过相对于当前项目根目录的相对路径加载资源
Resource resource = resourceLoader.getResource("file:src/main/resources/demo.xml");
//加载classpath下的资源
Resource resource = resourceLoader.getResource("classpath:demo.xml");
Resource resource = resourceLoader.getResource("classpath:com/example/spring/ResourceLoaderDemo.class");
//通过https url加载CSDN上的一篇博客资源  
Resource resource = resourceLoader.getResource("https://blog.csdn.net/wb_snail/article/details/108134550");
(tips:按通配符加载一组资源的方式,下文会有说明)

拿到Resource后,可以调用Resource#getInputStream获取资源输入流,读取其内容:

InputStream inputStream=resource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while (true) {
    String line = reader.readLine();
    if (line == null)
        break; 
    System.out.println(line);
} 
reader.close();

如果你想通过与Spring集成的方式使用ResourceLoader,可以这样操作:

1、实现ResourceLoaderAware接口,Spring容器启动过程中回调注入ResourceLoader实例

2、@Autowired ResourceLoader resourceLoader(Spring容器启动过程中,会向BeanFactory中注入一些特殊的对象(包括ResourceLoader对象 ),特殊是因为它们没有被定义为bean,它们是spring内部的组件,spring允许我们通过@Autowire来使用它们)

3、使用ApplicationContext,ApplicationContext继承于ResourceLoader,实际上通过前两种方式拿到的ResourceLoader对象,就是ApplicationContext(默认情况下,由于AbstractApplicationContext继承于DefaultResourceLoader,使用ApplicationContext进行资源加载时,最终还是会由DefaultResourceLoader执行)

Resource接口比较核心的方法有:

getInputStream():继承于InputStreamSource接口,返回资源对应的输入流,用于读取资源

exists():返回资源是否存在的标识

getFile():如果资源存在于文件系统中,返回对应的文件对象,否则抛出FileNotFoundException(比如ByteArrayResource这种只在内存中存在的资源)

getURL():返回资源对应的URL(java.net.URL),URL是资源定位符,上述例子中的"file:D:/demo.xml"、"classpath:demo.xml"、"https://blog.csdn.net/wb_snail/article/details/108134550"都是URL的String表示形式

Resource继承体系如下(体系比较大,这里只挑选了一些比较常见的Resource实现):

InputStreamSource:Resource的父接口,只有一个getInputStream()方法

WritableResource:可写资源,它的方法getOutputStream()可以返回资源的输出流

FileSystemResource:文件系统资源,可通过File对象、文件系统绝对路径、Path对象(如Paths.get("D:/demo.xml"))来构建(tips:Spring在处理@ComponentScan定义的包路径下的class文件时,会将它们加载为FileSystemResource)

ClassPathResource:classpath下的资源(tips:Spring在处理@PropertyResource、@PropertyResources时,相关配置文件会被加载为ClassPathResource)

UrlResource:引用了一个java.net.URL对象,可以访问任何可以用URL表示的资源(file、https、ftp等资源)

ServletContextResource:web应用资源,资源路径以相对于web应用根目录的路径表示,比如new ServletContextResource("/WEB-INF/demo.xml")

ByteArrayResource:通过一个二进制数组创建的资源,比如new ByteArrayResource(new String("hello").getBytes(StandardCharsets.UTF_8))

Resource几乎可以表示任何类型的底层资源,除了Spring已经实现的多种资源类型外,你还可以实现自己的Resource(比如DB中的资源),你可以向ResourceLoader中注册一个ProtocolResolver,然后就可以使用ResourceLoader以与其他类型资源无差别的方式加载你的资源。相关源码如下

public class CustomResource {
	public static void main(String[] args) {
		DefaultResourceLoader resourceLoader=new DefaultResourceLoader();
		//注册自定义ProtocolResolver
		resourceLoader.addProtocolResolver(new DbProtocolResolver());
		//通过"db:"为前缀的路径加载db中的资源
		Resource dbResource=resourceLoader.getResource("db:dataSource_fileDB/table_file/column_content");
	}
}

//自定义Resource
public class DbResource extends AbstractResource {
	private final String path;
    ...
}

/**
 * 实现ProtocolResolver,针对前缀为"db:"的url,返回DBResource
 */
public class DbProtocolResolver implements ProtocolResolver {
	
	private static final String DB_URL_PREFIX="db:";
	
	@Nullable
	public Resource resolve(String location, ResourceLoader resourceLoader){
		if(location.startsWith(DB_URL_PREFIX)){
			return new DbResource(location.substring(DB_URL_PREFIX.length()));
		}
		return null;
	}
}

//以下是DefaultResourceLoader中相关部分的源码
public class DefaultResourceLoader implements ResourceLoader {

    //一组ProtocolResolver实例
	private final Set<ProtocolResolver> protocolResolvers = new LinkedHashSet<>(4);

	public DefaultResourceLoader() {
	}

    //调用该方法,注册你自定义的ProtocolResolver
	public void addProtocolResolver(ProtocolResolver resolver) {
		Assert.notNull(resolver, "ProtocolResolver must not be null");
		this.protocolResolvers.add(resolver);
	}

	@Override
	public Resource getResource(String location) {
		Assert.notNull(location, "Location must not be null");

        //加载资源时,优先通过你注册的ProtocolResolver加载资源
		for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
			Resource resource = protocolResolver.resolve(location, this);
			if (resource != null) {
				return resource;
			}
		}

		if (location.startsWith("/")) {
			return getResourceByPath(location);
		}
		else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
			return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
		}
		else {
			try {
				// Try to parse the location as a URL...
				URL url = new URL(location);
				return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
			}
			catch (MalformedURLException ex) {
				// No URL -> resolve as resource path.
				return getResourceByPath(location);
			}
		}
	}
}

ResourceLoader仅支持匹配单个资源,其扩展接口ResourcePatternResolver支持以通配符的方式加载所有满足条件的资源,比如"classpath*:META-INF/spring.handlers"、"classpath:META-INF/*.properties"、"/WEB-INF/*-context.xml"、"file:D:/resources/*.properties"

注意"classpath:"和"classpath*:"的区别,后者会把classpath下所有jar包也作为查找目标,@ComponentScan可以扫描jar包下的@Component,正是利用了这个特性,Spring通过查找spring.handlers文件实现SPI也是一样道理

ResourcePatternResolver支持三种通配符:

*:匹配资源路径中的任意字符

?:匹配资源路径中的单个字符

如"*.xml"可以匹配到a.xml、ab.xml,而"?.xml"只能匹配到a.xml

**:匹配任意层级,比如"mapper/**/*Mapper.xml"可以匹配到mapper/RoleMapper.xml、mapper/order/OrderMapper.xml、mapper/order/goods/GoodsMapper.xml

Spring Framework中,ResourcePatternResolver的唯一有效实现是PathMatchingResourcePatternResolver,ApplicationContext也继承于ResourcePatternResolver,默认情况下,其getResources方法会委托给PathMatchingResourcePatternResolver执行

可以通过两种方式使用ResourcePatternResolver的能力 :

//使用PathMatchingResourcePatternResolver加载
ResourcePatternResolver resourcePatternResolver=new PathMatchingResourcePatternResolver();
Resource[] resources=resourcePatternResolver.getResources("classpath*:META-INF/spring.handlers");

//使用ApplicationContext加载
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
applicationContext.refresh();
Resource[] resources=applicationContext.getResources("classpath*:META-INF/spring.handlers");
  • 和ResourceLoader一样,ResourcePatternResolver同样支持脱离Spring容器使用,通过直接new PathMatchingResourcePatternResolver()的方式使用非常方便
  • 默认情况下,ApplicationContext#getResources方法会委托给PathMatchingResourcePatternResolver执行,因此只要可以拿到ApplicationContext实例,也可以进行基于正则的批量资源加载
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值