Spring中的DefaultResourceLoader使用方法解讀
DefaultResourceLoader的使用
1 前言
spring的DefaultResourceLoader類實現(xiàn)了ResourceLoader接口,可以方便讀取resources等目錄下的資源文件,存在于spring-core依賴中。
其他依賴如下:
<!-- 讀取File轉(zhuǎn)換為String -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
2 使用
resources的Demo目錄下準備測試test.json文件:

該包下,F(xiàn)ileUtils.readFileToString方法可將File文件轉(zhuǎn)換成String,其中需要設置字符集:
package com.xiaoxu.utils.File;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import java.io.File;
import java.io.IOException;
/**
* @author xiaoxu
* @date 2022-04-27
* spring_boot:com.xiaoxu.utils.File.FileUtils
*/
public class FileUtils {
public static String parseJsonFileToString(String caseJsonFilePath){
DefaultResourceLoader defaultResourceLoader = new DefaultResourceLoader();
Resource resource = defaultResourceLoader.getResource("classpath:" + caseJsonFilePath);
File file;
try {
file = resource.getFile();
} catch (IOException e) {
throw new RuntimeException(String.format("調(diào)用%s方法失敗,文件獲取失敗",
Thread.currentThread().getStackTrace()[1].getMethodName()));
}
String s;
try {
s = org.apache.commons.io.FileUtils.readFileToString(file, "utf-8");
} catch (IOException e) {
throw new RuntimeException(String.format("調(diào)用%s方法失敗,String獲取失敗,%s",
Thread.currentThread().getStackTrace()[1].getMethodName(),e.getMessage()));
}
return s;
}
public static void main(String[] args) {
System.out.println(parseJsonFileToString("Demo/test.json"));
}
}執(zhí)行結果如下,亦可結合fastjson使用:
{
"data": [
{"name": "你好","age": 30}
]
}
查看DefaultResourceLoader源碼可知,getResource方法讀取路徑時,開頭為classpath:的,會使用ClassPathResource類處理: 并且會截掉classpath:,
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
Iterator var2 = this.getProtocolResolvers().iterator();
Resource resource;
do {
if (!var2.hasNext()) {
if (location.startsWith("/")) {
return this.getResourceByPath(location);
}
if (location.startsWith("classpath:")) {
return new ClassPathResource(location.substring("classpath:".length()), this.getClassLoader());
}
try {
URL url = new URL(location);
return (Resource)(ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
} catch (MalformedURLException var5) {
return this.getResourceByPath(location);
}
}
ProtocolResolver protocolResolver = (ProtocolResolver)var2.next();
resource = protocolResolver.resolve(location, this);
} while(resource == null);
return resource;
}
在ClassPathResource類中,還調(diào)用了Spring的工具類StringUtils的cleanPath方法將\ \換成/,還有去掉開頭的/等等:
public ClassPathResource(String path, @Nullable ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
String pathToUse = StringUtils.cleanPath(path);
if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}
this.path = pathToUse;
this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
}到此這篇關于Spring中的DefaultResourceLoader使用方法解讀的文章就介紹到這了,更多相關Spring的DefaultResourceLoader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring?Boot教程之提高開發(fā)效率必備工具lombok
這篇文章主要介紹了Spring?Boot教程之提高開發(fā)效率必備工具lombok的相關資料,需要的朋友可以參考下2022-08-08
Java網(wǎng)絡編程之UDP網(wǎng)絡通信詳解
這篇文章主要為大家詳細介紹了Java網(wǎng)絡編程中的UDP網(wǎng)絡通信的原理與實現(xiàn),文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2022-09-09
Springboot Apollo配置yml的問題及解決方案
這篇文章主要介紹了Springboot Apollo配置yml的問題及解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
jpa多條件查詢重寫Specification的toPredicate方法
這篇文章主要介紹了多條件查詢重寫Specification的toPredicate方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

