Spring 中的 ResourceLoader實(shí)例詳解
ResourceLoader 是用來將所需要的資源加載 并封裝成 Resource對(duì)象
public void printStream(InputStream inputStream) throws IOException {
Reader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while ( (line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}1.DefaultResourceLoader
DefaultResourceLoader是 ResourceLoader 一個(gè)默認(rèn)的實(shí)現(xiàn),只能加載單個(gè)的資源
@Test
public void getResource() throws IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("beans.xml");
InputStream inputStream = resource.getInputStream();
printStream(inputStream);
}
@Test
public void getResourceByPath() throws IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("/com/fll/TestCase.class");
InputStream inputStream = resource.getInputStream();
printStream(inputStream);
}2.ResourcePatternResolver
ResourcePatternResolver也是繼承了ResourceLoader,但是ResourcePatternResolver又定義了一個(gè)加載多個(gè)資源的方法,就是按照一個(gè)Pattern加載所有符合的資源
PathMatchingResourcePatternResolver一個(gè)按照ant風(fēng)格的路徑進(jìn)行資源匹配加載的實(shí)現(xiàn)類
@Test
public void getResources() throws Exception {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resourcePatternResolver.getResources("/**");
Arrays.stream(resources).forEach(
resource -> {
if(resource.getFilename().equals("beans.xml")){
try {
InputStream inputStream = resource.getInputStream();
printStream(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}else {
System.out.println(resource.getFilename());
}
}
);
}/** 代表加載classpath下的所有資源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean name="person" class="com.fll.start.Person">
<property name = "name" value = "zhangsan"/>
<property name = "age" value = "20"/>
</bean>
</beans>
TestCase.class
AnnotationContainerStartProcess.class
Car.class
InputStreamPrinter.class
Person.class
XmlContainerStartProcess.class
DefaultResourceLoaderTest.class
InputResourceTest.class
ResourcePatternResolverTest.class
SpringStringUtilsTest.class
resource_loader
start
fll
com可以看到確實(shí)匹配到了所有的資源,包括目錄
到此這篇關(guān)于Spring 中的 ResourceLoader的文章就介紹到這了,更多相關(guān)Spring ResourceLoader內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例
這篇文章主要介紹了java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換
大家都知道在一些情況下,我們需要特殊的編碼格式,如:UTF-8,但是系統(tǒng)默認(rèn)的編碼為ISO-8859-1,遇到這個(gè)問題,該如何對(duì)字符串進(jìn)行兩個(gè)編碼的轉(zhuǎn)換呢,下面小編給大家分享下java中如何在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換,感興趣的朋友一起看看吧2021-12-12
SpringBoot+thymeleaf+Echarts+Mysql 實(shí)現(xiàn)數(shù)據(jù)可視化讀取的示例
本文主要介紹了SpringBoot+thymeleaf+Echarts+Mysql 實(shí)現(xiàn)數(shù)據(jù)可視化讀取的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Spring boot開發(fā)web應(yīng)用JPA過程解析
這篇文章主要介紹了Spring boot開發(fā)web應(yīng)用JPA過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot監(jiān)控API請(qǐng)求耗時(shí)的6中解決解決方案
本文介紹SpringBoot中記錄API請(qǐng)求耗時(shí)的6種方案,包括手動(dòng)埋點(diǎn)、AOP切面、攔截器、Filter、事件監(jiān)聽、Micrometer+Prometheus,具有一定的參考價(jià)值,感興趣的可以了解一下2025-07-07
Java構(gòu)造方法實(shí)例詳解(動(dòng)力節(jié)點(diǎn)java學(xué)院整理)
其實(shí)java構(gòu)造方法很簡單,下面通過示例給大家分享java構(gòu)造方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-04-04

