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

Spring 中的 ResourceLoader實例詳解

 更新時間:2024年11月05日 10:17:16   作者:楊過姑父  
Spring框架提供了ResourceLoader接口,用于加載資源文件,DefaultResourceLoader是其基本實現(xiàn),只能加載單個資源,而ResourcePatternResolver繼承自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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論