重叠泪痕缄锦字,人生只有情难死。
![676ff25df012e35a649603a8b1e64bd9.png](https://i-blog.csdnimg.cn/blog_migrate/2266dba1e926bcb3e88c2039a3e75892.jpeg)
概述
本文将了解资源或文件(例如文本文件、XML文件、属性文件或图像文件)加载到Spring应用程序上下文中的不同实现。Spring ResourceLoader为我们提供了一个统一的getResource()方法来通过资源路径检索外部资源。
资源(Resource)接口
Resource是Spring中用于表示外部资源的通用接口。
Spring为Resource接口提供了以下6种实现。
- UrlResource
- ClassPathResource
- FileSystemResource
- ServletContextResource
- InputStreamResource
- ByteArrayResource
我们可以指定不同的前缀来创建路径以从不同位置加载资源
前缀示例说明classpath:classpath:com/myapp/config.xml从类路径加载file:file:///data/config.xml从文件系统作为URL加载。http:https://myserver/logo.png从URL加载(none)/data/config.xml取决于底层的ApplicationContext
ResourceLoader
它用于加载资源(例如类路径或文件系统资源)。它有两种方法:
//Expose the ClassLo