webx学习(四)——ResourceLoadingService

ResourceLoadingService是一个可以从各种输入源中(例如从File System、Classpath、Webapp中)查找和读取资源文件的服务。
资源表现形式的多样性,给应用程序的接口设计带来一点麻烦,为了统一资源的获取,Spring框架中提供了这方面的服务,即Resource Loader,但是Resource Loader还存在一些不合理的地方,于是webx中提供了Resource Loading Service对资源进行统一管理,在Resource Loading Service中可以包含多个不同的Resource Loader进行资源的加载,使得加载资源具有多样性,同时也很好的完成了资源加载的大部分功能。

ResourceLoadingService是从 Spring的ResourceLoader派生过来的。

你只需要在配置文件中增加以下内容,就可以将Spring ResourceLoader机制替换成Webx的Resource Loading服务:

Resource Loading服务的基本配置(/WEB-INF/webx.xml)
 
<resource-loading
         xmlns= "http://www.alibaba.com/schema/services"
 
     <resource-alias pattern= "/" name= "/webroot" />
 
     <resource pattern= "/webroot" internal= "true" >
         <res-loaders:webapp-loader />
     </resource>
     <resource pattern= "/classpath" internal= "true" >
         <res-loaders:classpath-loader />
     </resource>
 
</ resource-loading>

1.定义新资源:

<resource pattern= "/jdk" >
         <res-loaders:file-loader basedir= "${java.home}" />
  </resource>

定义新资源,资源名以/jdk为前缀。
<file-loader>表示从文件系统中装载资源。

2.重命名资源

<resource-alias pattern= "/myapp/conf" name= "/webroot/WEB-INF" />
 
<resource pattern= "/webroot" internal= "true" >
     <res-loaders:webapp-loader />
</resource>

定义了一个资源的别名:/myapp/conf。
当你查找/myapp/conf/myFile.xml时,Resource Loading服务实际上会去找/webroot/WEB-INF/myFile.xml

internal=true是一个可选项,当它的值为true时,代表它所修饰的资源是不能被外界所直接访问的。例如,你想直接在myBean中注入/webroot/WEB-INF/myFile.xml是不行的。把internal选项设成true,可以让强制用户转向新的资源名称。Internal参数的默认值为false,意味着,新旧两种名称同时可用。

3.重定向资源

<resource-alias pattern= "/templates" name= "/webroot/templates" />
 
     <resource pattern= "/templates/cms" >
         <res-loaders:file-loader basedir= "${cms_root}" />
     </resource>
 
     <resource pattern= "/webroot" internal= "true" >
         <res-loaders:webapp-loader />
     </resource>

定义了一个资源的别名:/templates,指向internal资源:/webroot/templates。
将/templates的子目录/templates/cms重定向到某个外部的文件目录$cms_root中

举几个例子:

将/myapp/conf/my/file.xml转换成/webroot/WEB-INF/my/file.xml。
<resource-alias pattern="/myapp/conf/*/.xml" name="/webroot/WEB-INF/$1/$2.xml" />

将/myapp/conf/myfile.conf转换成/webroot/WEB-INF/myfile.xml。
<resource-alias pattern="/myapp/conf/*.conf" name="/WEB-INF/$1.xml"/>

将/profiles/myname转换成文件路径${profile_root}/m/myname;将/profiles/othername转换成文件路径${profile_root}/o/othername。
<resource pattern="/profiles/?*">
<res-loaders:file-loader basedir="${profile_root}">
<res-loaders:path>$1/$1$2</res-loaders:path>
</res-loaders:file-loader>
</resource>


每个Spring容器都可以配置自己的Resource Loading服务。
当调用子容器的Resource Loading服务时,遵循这样的逻辑:
先在子容器的Resource Loading服务中查找资源,如果找不到,
则再到parent容器的Resource Loading服务中查找,如果找不到,则放弃。

运用这种级联装载资源的方法,子应用可以把共享的资源定义在root context中,而把自己独享的资源定义在自己的容器当中。

资源文件里的内容不仅可以读取出来,ResourceLoadingService还可以修改资源文件的内容:

<resource-filters pattern= "test-*.xml" >
         <res-filters:xslt-filter xslt= "/stylesheet.for.test/test.xsl" saveTo= "/tempdir" />
     </resource-filters>
 
     <resource pattern= "/tempdir" >
         <loaders:file-loader basedir= "${project.home}/target/test" />
     </resource>

将所有目录下(因为是相对路径)的名称为test-*.xml文件,用指定的XSL文件进行转换。
这里引进了一种新的扩展点:ResourceFilter。ResourceFilter可以在应用获取资源之前,取得控制,以便对资源做一点事。
<xslt-filter>是对ResourceFilter的扩展,它能够把XML资源用指定的xsl文件转换成新的格式。假如指定了saveTo参数,就可以把转换的结果保存下来,避免每次访问都重新转换。

此处定义tempdir目录资源,以便保存xslt转换的结果。
-------------------------------------------------------------------------------------------------------------------------------------------------------

ResourceLoader参考:

FileResourceLoader

<resource pattern= "/my/virtual" >
              <res-loaders:file-loader />
           </resource>

file-loader会从当前配置文件所在的目录中装载

<resource pattern= "/my/virtual" >
               <res-loaders:file-loader basedir= "${my.basedir}" />
           </resource>

这样,它就会从指定的basedir的子目录中查找资源。

WebappResourceLoader:

<resource pattern= "/my/virtual" >
               <res-loaders:webapp-loader />
           </resource>

从当前WEB应用中装载资源,也就是从ServletContext对象中装载资源。

ClasspathResourceLoader:

<resource pattern= "/my/virtual" >
               <res-loaders:classpath-loader />
           </resource>

从classpath中装载资源,也就是从当前的ClassLoader对象中装载资源。

SuperResourceLoader:
调用Resource Loading服务来取得资源。它有点像Java里面的super操作符。

<resource pattern= "/my/virtual" >
               <res-loaders: super -loader basedir= "/webroot/WEB-INF" />
           </resource>

这个操作类似于<resource-alias>。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值