欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中的DefaultResourceLoader使用方法解讀

 更新時(shí)間:2024年02月02日 08:45:25   作者:小徐也要努力鴨  
這篇文章主要介紹了Spring中的DefaultResourceLoader使用方法解讀,DefaultResourceLoader是spring提供的一個默認(rèn)的資源加載器,DefaultResourceLoader實(shí)現(xiàn)了ResourceLoader接口,提供了基本的資源加載能力,需要的朋友可以參考下

DefaultResourceLoader的使用

1 前言

spring的DefaultResourceLoader類實(shí)現(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目錄下準(zhǔn)備測試test.json文件:

在這里插入圖片描述

該包下,F(xiàn)ileUtils.readFileToString方法可將File文件轉(zhuǎn)換成String,其中需要設(shè)置字符集:

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í)行結(jié)果如下,亦可結(jié)合fastjson使用:

{
  "data": [
    {"name": "你好","age": 30}
  ]
}

查看DefaultResourceLoader源碼可知,getResource方法讀取路徑時(shí),開頭為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();
}

到此這篇關(guān)于Spring中的DefaultResourceLoader使用方法解讀的文章就介紹到這了,更多相關(guān)Spring的DefaultResourceLoader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot添加License的多種方式

    SpringBoot添加License的多種方式

    License指的是版權(quán)許可證,當(dāng)我們開發(fā)完系統(tǒng)后,如果不想讓用戶一直白嫖使用,比如說按時(shí)間續(xù)費(fèi),License的作用就有了。我們可以給系統(tǒng)指定License的有效期,控制系統(tǒng)的可用時(shí)間。
    2021-06-06
  • Java設(shè)計(jì)模式之中介模式

    Java設(shè)計(jì)模式之中介模式

    這篇文章主要介紹了Java設(shè)計(jì)模式之中介模式,中介模式(Mediator?Pattern),屬于行為型設(shè)計(jì)模式,目的是把系統(tǒng)中對象之間的調(diào)用關(guān)系從一對多轉(zhuǎn)變成一對一的調(diào)用關(guān)系,以此來降低多個對象和類之間的通信復(fù)雜性,需要的朋友可以參考下
    2023-12-12
  • Spring?Boot教程之提高開發(fā)效率必備工具lombok

    Spring?Boot教程之提高開發(fā)效率必備工具lombok

    這篇文章主要介紹了Spring?Boot教程之提高開發(fā)效率必備工具lombok的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • 淺聊一下Spring?Security的使用方法

    淺聊一下Spring?Security的使用方法

    Spring?Security?是一個基于?Spring?框架的安全框架,提供了一套安全性認(rèn)證和授權(quán)的解決方案,用于保護(hù)?Web?應(yīng)用程序和服務(wù),接下來小編就和大家聊聊Spring?Security,感興趣的小伙伴跟著小編一起來看看吧
    2023-08-08
  • Java fastjson2 解析JSON用法詳解

    Java fastjson2 解析JSON用法詳解

    Fastjson2是Fastjson的升級版,提供了更好的性能和擴(kuò)展性,它支持多種方式解析JSON數(shù)據(jù),包括將JSON字符串轉(zhuǎn)換為Java對象、嵌套的JSON對象和數(shù)組,以及轉(zhuǎn)換成Map或List,本文介紹Java fastjson2 解析JSON用法,感興趣的朋友一起看看吧
    2025-02-02
  • Java網(wǎng)絡(luò)編程之UDP網(wǎng)絡(luò)通信詳解

    Java網(wǎng)絡(luò)編程之UDP網(wǎng)絡(luò)通信詳解

    這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程中的UDP網(wǎng)絡(luò)通信的原理與實(shí)現(xiàn),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-09-09
  • 一文帶你搞懂Java單例模式

    一文帶你搞懂Java單例模式

    單例就是單實(shí)例的意思,即在系統(tǒng)全局,一個類只創(chuàng)建一個對象,并且在系統(tǒng)全局都可以訪問這個對象而不用重新創(chuàng)建。本文將通過示例為大家詳細(xì)講解Java單例模式的使用,需要的可以參考一下
    2022-11-11
  • Spring?Security安全框架之記住我功能

    Spring?Security安全框架之記住我功能

    這篇文章主要介紹了Spring?Security安全框架之記住我,本次就來探究如何實(shí)現(xiàn)這種自動登錄、記住我的功能,通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • Springboot Apollo配置yml的問題及解決方案

    Springboot Apollo配置yml的問題及解決方案

    這篇文章主要介紹了Springboot Apollo配置yml的問題及解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • jpa多條件查詢重寫Specification的toPredicate方法

    jpa多條件查詢重寫Specification的toPredicate方法

    這篇文章主要介紹了多條件查詢重寫Specification的toPredicate方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論