Spring 中的 ResourceLoader實例詳解
ResourceLoader 是用來將所需要的資源加載 并封裝成 Resource對象
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 一個默認的實現(xiàn),只能加載單個的資源
@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又定義了一個加載多個資源的方法,就是按照一個Pattern加載所有符合的資源
PathMatchingResourcePatternResolver一個按照ant風格的路徑進行資源匹配加載的實現(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
可以看到確實匹配到了所有的資源,包括目錄
到此這篇關于Spring 中的 ResourceLoader的文章就介紹到這了,更多相關Spring ResourceLoader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Easyui的combobox實現(xiàn)動態(tài)數(shù)據(jù)級聯(lián)效果
這篇文章主要介紹了Easyui的combobox實現(xiàn)動態(tài)數(shù)據(jù)級聯(lián)效果的相關資料,感興趣的小伙伴們可以參考一下2016-06-06SpringBoot 3.0 新特性內(nèi)置聲明式HTTP客戶端實例詳解
聲明式 http 客戶端主旨是使得編寫 java http 客戶端更容易,為了貫徹這個理念,采用了通過處理注解來自動生成請求的方式,本文給大家詳解介紹SpringBoot 聲明式HTTP客戶端相關知識,感興趣的朋友跟隨小編一起看看吧2022-12-12自定義的Troop<T>泛型類( c++, java和c#)的實現(xiàn)代碼
這篇文章主要介紹了自定義的Troop<T>泛型類( c++, java和c#)的實現(xiàn)代碼的相關資料,需要的朋友可以參考下2017-05-05在SpringBoot中實現(xiàn)多種方式登錄(通過用戶名、手機號、郵箱等)的詳細指南
今天,我們將跳進 Spring Boot 的世界,探索如何通過 用戶名、手機號、郵箱 等多種方式實現(xiàn)登錄,而我們要做的就是為他們提供這些選擇,確保他們都能毫無阻礙地進入我們的系統(tǒng),感興趣的小伙伴跟著小編一起來看看吧2024-11-11springboot 在idea中實現(xiàn)熱部署的方法
這篇文章主要介紹了springboot 在idea中實現(xiàn)熱部署的方法,實現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下2018-10-10