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

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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架中,ResourceLoader是一个用于加载资源的接口。它提供了一种统一的方式来加载各种类型的资源文件,比如类路径上的文件、文件系统中的文件、URL资源等。 ResourceLoader接口定义了一个getResource(String location)方法,用于获取资源对象。具体的实现类包括ClassPathResourceLoader、FileSystemResourceLoader和UrlResourceLoader等。 使用ResourceLoader可以方便地加载资源文件,并且可以处理多种类型的资源路径。下面是一个示例代码,演示如何使用ResourceLoader加载资源文件: ```java import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class Main { private ResourceLoader resourceLoader; public Main(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public void loadResource(String location) { Resource resource = resourceLoader.getResource(location); // 使用resource对象进行操作,比如读取内容等 } public static void main(String[] args) { // 创建Spring并获取ResourceLoader实例 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ResourceLoader resourceLoader = context; // 使用ResourceLoader加载资源文件 Main main = new Main(resourceLoader); main.loadResource("classpath:config.properties"); } } ``` 在上述示例中,我们通过Spring获取了一个ResourceLoader实例,然后调用它的getResource方法来加载资源文件。可以通过不同的前缀来指定不同的资源类型,比如"classpath:"表示类路径上的资源文件。 使用SpringResourceLoader可以更灵活地处理资源加载,并且可以适应不同的资源路径。它是Spring框架中用于加载资源的重要组件之一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值