Spring源码阅读总结-03-ResourceLoader资源加载器-03

本文详细探讨了Spring中的ResourceLoader组件,包括ResourcePatternResolver子类PathMatchingResourcePatternResolver的功能,以及findPathMatchingResources和findAllClassPathResources方法在资源查找中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1 ResourcePatternResolver

ResourceLoaderResource getResource(String location),每次只能根据 location 返回一个 ResourceResourcePatternResolverResourceLoader 的扩展,它支持根据指定的资源路径匹配模式每次返回多个 Resource 实例

org.springframework.core.io.support;
// ResourceLoader 的扩展
public interface ResourcePatternResolver extends ResourceLoader {
   

	String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

	// 根据指定的资源路径匹配模式每次返回多个 Resource 实例
	Resource[] getResources(String locationPattern) throws IOException;

}

1.2 PathMatchingResourcePatternResolver

ResourcePatternResolver 最常用的子类
支持 ResourceLoaderResourcePatternResolver 新增的 "classpath*:" 通配符前缀外,还支持 Ant风格的路径匹配模式(Ant风格类似于 "**/*.xml")


org.springframework.core.io.support;
// 继承 ResourcePatternResolver
public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
   
	// 资源定位器
	private final ResourceLoader resourceLoader;

	// Ant 路径匹配器 指 以 /开头的路径 如:/**/example
	private PathMatcher pathMatcher = new AntPathMatcher();

	// 默认构造
	public PathMatchingResourcePatternResolver() {
   
		this.resourceLoader = new DefaultResourceLoader();
	}

	// 指定 ResourceLoader 构造
	public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
   
		Assert.notNull(resourceLoader, "ResourceLoader must not be null");
		this.resourceLoader = resourceLoader;
	}
	
	// 指定 ClassLoader 构造
	public PathMatchingResourcePatternResolver(@Nullable ClassLoader classLoader) {
   
		this.resourceLoader = new DefaultResourceLoader(classLoader);
	}

	// 根据路径返回单个资源对象, 实现 ResourceLoader中的
	@Override
	public 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值