Spring 提供了簡單的接口來管理資源,并支持多種資源類型。
一忆嗜、Resource 接口
Java 自帶的 java.net.URL
類只能處理 URL 前綴的資源,不能訪問更多類型的低級資源陕习。
因此霎褐,Spring 提供了 Resource
接口以及多種資源類型的實現(xiàn)。
public static void main(String[] args) throws Exception {
// Resource
Resource resource = new ClassPathResource("test/test-one.txt");
System.out.println("Filename: " + resource.getFilename());
System.out.println("Description: " + resource.getDescription());
System.out.println("URI: " + resource.getURI());
System.out.println("URL: " + resource.getURL());
System.out.println("Data: " + streamToString(resource));
System.out.println();
}
// Stream to String
private static String streamToString(Resource resource) throws IOException {
return StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
}
1. 內(nèi)置實現(xiàn)
Spring 提供了下面幾種資源實現(xiàn):
UrlResource
UrlResource
包裝了 java.net.URL
類该镣,通過 URL 訪問資源,例如文件响谓、HTTP损合、FTP 資源。
通過協(xié)議前綴區(qū)分資源類型娘纷,例如 file:
訪問文件系統(tǒng)資源嫁审,還有 http:
、ftp:
等赖晶。
public static void main(String[] args) throws Exception {
// UrlResource
UrlResource fileResource = new UrlResource("file:E:/codeartist/spring/spring-ioc/src/main/resources/test/test-one.txt");
UrlResource httpResource = new UrlResource("https://www.baidu.com/");
}
ClassPathResource
ClassPathResource
可以從 classpath 下獲取資源律适。
public static void main(String[] args) throws Exception {
// ClassPathResource
ClassPathResource classPathResource = new ClassPathResource("test/test-one.txt");
}
FileSystemResource
FileSystemResource
實現(xiàn)了對 java.io.File
資源的處理,支持 File
和文件路徑指定資源遏插。
public static void main(String[] args) throws Exception {
// FileSystemResource
FileSystemResource fileSystemResource =
new FileSystemResource("E:/codeartist/spring/spring-ioc/src/main/resources/test/test-two.txt");
}
ServletContextResource
ServletContextResource
可以管理來自 ServletContext
的資源捂贿。
InputStreamResource
InputStreamResource
可以管理來自 InputStream
生成的資源。
ByteArrayResource
ByteArrayResource
可以管理來自 Byte 數(shù)組生成的資源胳嘲。
如果是從 Jar 包里面獲取資源厂僧,不能通過 File 形式獲取,只能通過流來獲取了牛。
2. 協(xié)議前綴
前綴 | 示例 | 描述 |
---|---|---|
classpath: | classpath:myapp/config.xml |
加載來自 classpath 的資源 |
file: | file:///data/config.xml |
通過 URL 加載來自文件系統(tǒng)的資源 |
http: | http://myserver/logo.png |
通過 URL 加載來自網(wǎng)絡的資源 |
(none) | /data/config.xml |
依賴底層的 ApplicationContext
|
二颜屠、ResourceLoader 接口
ResourceLoader
接口用來加載 Resource
辰妙,通過協(xié)議前綴來區(qū)分資源的類型。
所有的 Application Context 都實現(xiàn)了 ResourceLoader
接口甫窟,當使用 Application Context 來加載資源的時候密浑,如果沒有指定協(xié)議前綴,根據(jù) Application Context 的類型來指定從哪里加載粗井。
-
ClassPathXmlApplicationContext
加載ClassPathResource
-
FileSystemXmlApplicationContext
加載FileSystemResource
-
WebApplicationContext
加載ServletContextResource
在 Bean 中可以使用 ResourceLoaderAware
注入當前上下文的 ResourceLoader
尔破。
1. 資源注入
Spring 還支持通過 @Value
注解來注入資源,通過協(xié)議前綴來注入背传。
@Component
public class BeanExample {
@Value("classpath:test/test-one.txt")
private Resource resource;
@Value("classpath:test/test-two.txt")
private File file;
}
支持注入的字段類型有:File
呆瞻、Resource
等。
2. Application Context 資源
通過 Application Context 加載單個資源径玖。
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext("cn.codeartist.spring.resource");
Resource resource = applicationContext.getResource("classpath:test/test-one.txt");
}
Ant-style 風格
Application Context 通過 Ant-style 正則表達式加載多個資源痴脾。
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext("cn.codeartist.spring.resource");
Resource[] resources = applicationContext.getResources("classpath:test/test-*.txt");
for (Resource res : resources) {
// do something
}
}
classpath*: 前綴
Application Context 通過 classpath*:
前綴加載多個資源,該方式加載速度慢梳星,一般很少使用赞赖。
classpath:
表示只加載第一個 classpath 路徑下的資源。
classpath*:
表示加載系統(tǒng)中所有 classpath 路徑下的資源冤灾。
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext("cn.codeartist.spring.resource");
Resource[] resources = applicationContext.getResources("classpath*:**/test-one.txt");
for (Resource res : resources) {
// do something
}
}
ClassPathXmlApplicationContext
和 WebApplicationContext
等 Bean 配置文件資源都是基于 ResourceLoader
接口來加載的前域。
三、附錄
1. 示例代碼
Gitee 倉庫:https://gitee.com/code_artist/spring
項目模塊:spring-ioc
示例路徑:cn.codeartist.spring.resource
最新文章關注 CodeArtist 碼匠公眾號韵吨。
更多:Spring高效實踐專欄