ResourceLoader的用法

后面可以看下:ResourceUtils
学习连接:
第04篇:Resources资源文件处理,再也不怕找不到文件了。
Spring5中文文档【12】核心之Resources资源

ResourceLoader

public interface ResourceLoader {

	/** 从类路径中加载的伪URL路径: "classpath:". */
	String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;


	/**
	 * 返回指定资源位置的资源handle句柄
	 * handle句柄应该始终是可重用的在源描述符,允许多个 Resource#getInputStream()调用。
	 * 必须支持完全限定的URL,例如:"file:C:/test.dat"
	 * 必须支持classpath类路径伪URL,例如:"classpath:test.dat"
	 * 应该支持相对文件路径,例如:"WEB-INF/test.dat"
	 * 这将是特定实现的,通常由ApplicationContext实现提供
	 * 注意:资源handle句柄并不意味着现有资源,你需要调用Resource#exists来检查是否存在
	 * @param 资源位置
	 * @return a corresponding Resource handle (never {@code null})
	 * @see #CLASSPATH_URL_PREFIX
	 * @see Resource#exists()
	 * @see Resource#getInputStream()
	 */
	Resource getResource(String location);

	/**
	 * 返回ResourceLoader资源加载器使用的类加载器
	 */
	@Nullable
	ClassLoader getClassLoader();

}


public class ResourceTest {
    @Test
    public void test_DefaultResourceLoader() {

        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();

        Resource unknownRes = resourceLoader.getResource("classpath:unknown.txt");
        System.out.println(unknownRes.exists()); // false

        Resource r1 = resourceLoader.getResource("classpath:file/abc.txt");
        System.out.println(r1.exists()); // true

        Resource r2 = resourceLoader.getResource("classpath:/file/abc.txt");
        System.out.println(r2.exists()); // true

        Resource r3 = resourceLoader.getResource("file:file/abc.txt");
        System.out.println(r3.exists()); // false

        Resource r4 = resourceLoader.getResource("file:/file/abc.txt");
        System.out.println(r4.exists()); // false

        Resource r5 = resourceLoader.getResource("file:d:\\remark.txt");
        System.out.println(r5.exists()); // true

        Resource r5_2 = resourceLoader.getResource("file:d:/remark.txt");
        System.out.println(r5_2.exists()); // true

        Resource r5_3 = resourceLoader.getResource("file:/d:/remark.txt");
        System.out.println(r5_3.exists()); // true

        Resource r6 = resourceLoader.getResource("file:D:\\Projects\\practice\\" +
                "demo-webmvc\\src\\main\\resources\\file\\abc.txt");
        System.out.println(r6.exists()); // true

        Resource r7 = resourceLoader.getResource("/file/abc.txt");
        System.out.println(r7.exists()); // true

        Resource r8 = resourceLoader.getResource("file/abc.txt");
        System.out.println(r8.exists()); // true


    }

    @Test
    public void test_DefaultResourceLoader2() throws IOException {
        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath:META-INF/spring.factories");
        File file = resource.getFile(); // 抛出异常(不能加载引入的jar包下的文件)
        System.out.println(file.getName());
    }
    
    @Test
    public void test_DefaultResourceLoader2_1() throws IOException {
        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath*:META-INF/spring.factories");
        File file = resource.getFile(); // 抛出异常(连自己创建的文件都加载不到)
        System.out.println(file.getName());
    }

    @Test
    public void test_ResourcePatternResolver3() throws IOException {
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource resource = resourcePatternResolver.getResource("classpath:META-INF/spring.factories");
        Properties properties = new Properties();
        properties.load(resource.getInputStream());
        System.out.println(properties); // 能正常加载到jar包下的文件(同时存在,优先加载自己创建的)
    }

    @Test
    public void test_ResourcePatternResolver3_2() throws IOException {
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("classpath:META-INF/spring.factories");
        for (Resource resource : resources) {
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            System.out.println(properties); // 能正常加载到jar包下的文件,这里只加载到了1个(同时存在,优先加载自己创建的)
        }
    }

    @Test
    public void test_ResourcePatternResolver3_3() throws IOException {
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("classpath*:META-INF/spring.factories");
        for (Resource resource : resources) {
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            System.out.println(properties); // 能正常加载到jar包下的文件,这里加载了2个(同时存在,优先加载自己创建的)
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值