我是靠谱客的博主 大方黄蜂,这篇文章主要介绍ResourceLoader的用法,现在分享给大家,希望可以做个参考。

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

ResourceLoader

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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(); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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个(同时存在,优先加载自己创建的) } } }

最后

以上就是大方黄蜂最近收集整理的关于ResourceLoader的用法的全部内容,更多相关ResourceLoader内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部