美文网首页
webx笔记-ResourceLoadingService使用

webx笔记-ResourceLoadingService使用

作者: 兴浩 | 来源:发表于2018-10-06 23:15 被阅读12次

    接上篇:
    webx笔记-Resource

    在实际开发中,ResourceLoadingService是直接接触最多的接口,其屏蔽了内部的Loader和Resource相关细节

    1.ResourceLoadingService接口定义

    相比之前这么多的ResourceLoader,一个接口全部搞定

    测试代码

        @Test
        public void serviceTest() throws IOException {
            resourceLoadingService = (ResourceLoadingService) factory.getBean("testResource");
            Resource res = resourceLoadingService.getResource("test.txt");
            // 取得资源文件
            File file = resourceLoadingService.getResourceAsFile("test.txt");
            // 取得资源URL
            URL url = resourceLoadingService.getResourceAsURL("test.txt");
            // 取得资源输入流
            InputStream stream = resourceLoadingService.getResourceAsStream("test.txt");
        }
    

    ResourceLoadingService可以对标spring的DefaultResourceLoader

    DefaultResourceLoader的测试代码

        @Test
        public void testResourceLoader() throws IOException {
            ResourceLoader loader = new DefaultResourceLoader();
            Resource resource = loader.getResource("http://m2.xunbaozl.com/wxHome/selete.html");
            System.out.println(resource instanceof UrlResource); //true
            printContent(resource.getInputStream());
            //注意这里前缀不能使用“classpath*:”,这样不能真正访问到对应的资源,exists()返回false
            resource = loader.getResource("classpath:core/resources/test.txt");
            System.out.println(resource instanceof ClassPathResource); //true
            printContent(resource.getInputStream());
    //        resource = loader.getResource(System.getProperty("user.dir")+"/src/main/resources/core/resources/test.txt");
    //        System.out.println(resource instanceof ClassPathResource); //true
    //        printContent(resource.getInputStream());
        }
    

    当然ResourceLoadingService(下面称RLS,简写方便)的实现远远没这么简单,从以上示例可以隐约感受到RLS肯定与ResourceLoader(webx)存在某种关联关系,可以说RLS是其ResourceLoading的集大成者,包括了很多相关联的功能,但对外只暴露一个接口服务,下面就慢慢剖析

    2.RLS的定义

    webx以解析schema的方式来定义RLS对象,如下定义

    services:resource-loading表示RLS对象

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:services="http://www.alibaba.com/schema/services"
                 xmlns:filters="http://www.alibaba.com/schema/services/resource-loading/filters"
                 xmlns:loaders="http://www.alibaba.com/schema/services/resource-loading/loaders"
                 xsi:schemaLocation="
                     http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd
                     http://www.alibaba.com/schema/services/resource-loading/filters http://localhost:8080/schema/services-resource-loading-filters.xsd
                     http://www.alibaba.com/schema/services/resource-loading/loaders http://localhost:8080/schema/services-resource-loading-loaders.xsd
                     http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd
                 ">
        <services:resource-loading id="testResource">
            
        </services:resource-loading>
    </beans:beans>
    

    3.资源的定义

    如果只定义services:resource-loading标签的话,还无法使用相关接口方法查找资源,必须还要定义相关资源

    3.1 定义WebappResourceLoader

    使用resource定义资源,之后就可以加载其目录下的资源了

        <services:resource-loading>
            <resource pattern="/"  internal="false">
                <loaders:webapp-loader />
            </resource>
        </services:resource-loading>
    

    3.2 别名资源

    使用resource-alias定义一个别名资源

        <services:resource-loading>
            <resource pattern="/"  internal="false">
                <loaders:webapp-loader />
            </resource>
            <resource-alias pattern="/specFolder" name="/myfolder" />
        </services:resource-loading>
    

    当访问specFolder目录时,实际访问的则是myfolder
    如下示例

    3.3 重定向资源

    以下配置将/tf/cms重定向到myfolder目录下

        <services:resource-loading id="testResource">
            <resource pattern="/tf/cms">
                <loaders:file-loader basedir="../myfolder" />
            </resource>
        </services:resource-loading>
    

    其他的可以参考webx文档

    相关文章

      网友评论

          本文标题:webx笔记-ResourceLoadingService使用

          本文链接:https://www.haomeiwen.com/subject/zpahaftx.html