webx学习(四)——ResourceLoadingService
ResourceLoadingService是一個(gè)可以從各種輸入源中(例如從File System、Classpath、Webapp中)查找和讀取資源文件的服務(wù)。
資源表現(xiàn)形式的多樣性,給應(yīng)用程序的接口設(shè)計(jì)帶來(lái)一點(diǎn)麻煩,為了統(tǒng)一資源的獲取,Spring框架中提供了這方面的服務(wù),即Resource Loader,但是Resource Loader還存在一些不合理的地方,于是webx中提供了Resource Loading Service對(duì)資源進(jìn)行統(tǒng)一管理,在Resource Loading Service中可以包含多個(gè)不同的Resource Loader進(jìn)行資源的加載,使得加載資源具有多樣性,同時(shí)也很好的完成了資源加載的大部分功能。
ResourceLoadingService是從 Spring的ResourceLoader派生過(guò)來(lái)的。
你只需要在配置文件中增加以下內(nèi)容,就可以將Spring ResourceLoader機(jī)制替換成Webx的Resource Loading服務(wù):
| Resource Loading服務(wù)的基本配置(/WEB-INF/webx.xml) <resource-loading ????????xmlns="http://www.alibaba.com/schema/services" ????????xmlns:res-loaders="http://www.alibaba.com/schema/services/resource-loading/loaders"> ????<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>表示從文件系統(tǒng)中裝載資源。
2.重命名資源
| <resource-alias pattern="/myapp/conf" name="/webroot/WEB-INF" /> <resource pattern="/webroot" internal="true"> ????<res-loaders:webapp-loader /> </resource> |
定義了一個(gè)資源的別名:/myapp/conf。
當(dāng)你查找/myapp/conf/myFile.xml時(shí),Resource Loading服務(wù)實(shí)際上會(huì)去找/webroot/WEB-INF/myFile.xml
internal=true是一個(gè)可選項(xiàng),當(dāng)它的值為true時(shí),代表它所修飾的資源是不能被外界所直接訪問(wèn)的。例如,你想直接在myBean中注入/webroot/WEB-INF/myFile.xml是不行的。把internal選項(xiàng)設(shè)成true,可以讓強(qiáng)制用戶轉(zhuǎn)向新的資源名稱。Internal參數(shù)的默認(rèn)值為false,意味著,新舊兩種名稱同時(shí)可用。
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> |
定義了一個(gè)資源的別名:/templates,指向internal資源:/webroot/templates。
將/templates的子目錄/templates/cms重定向到某個(gè)外部的文件目錄$cms_root中
| 舉幾個(gè)例子: 將/myapp/conf/my/file.xml轉(zhuǎn)換成/webroot/WEB-INF/my/file.xml。 將/myapp/conf/myfile.conf轉(zhuǎn)換成/webroot/WEB-INF/myfile.xml。 將/profiles/myname轉(zhuǎn)換成文件路徑${profile_root}/m/myname;將/profiles/othername轉(zhuǎn)換成文件路徑${profile_root}/o/othername。 |
每個(gè)Spring容器都可以配置自己的Resource Loading服務(wù)。
當(dāng)調(diào)用子容器的Resource Loading服務(wù)時(shí),遵循這樣的邏輯:
先在子容器的Resource Loading服務(wù)中查找資源,如果找不到,
則再到parent容器的Resource Loading服務(wù)中查找,如果找不到,則放棄。
| 運(yùn)用這種級(jí)聯(lián)裝載資源的方法,子應(yīng)用可以把共享的資源定義在root context中,而把自己獨(dú)享的資源定義在自己的容器當(dāng)中。 |
資源文件里的內(nèi)容不僅可以讀取出來(lái),ResourceLoadingService還可以修改資源文件的內(nèi)容:
| <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> |
將所有目錄下(因?yàn)槭窍鄬?duì)路徑)的名稱為test-*.xml文件,用指定的XSL文件進(jìn)行轉(zhuǎn)換。
這里引進(jìn)了一種新的擴(kuò)展點(diǎn):ResourceFilter。ResourceFilter可以在應(yīng)用獲取資源之前,取得控制,以便對(duì)資源做一點(diǎn)事。
<xslt-filter>是對(duì)ResourceFilter的擴(kuò)展,它能夠把XML資源用指定的xsl文件轉(zhuǎn)換成新的格式。假如指定了saveTo參數(shù),就可以把轉(zhuǎn)換的結(jié)果保存下來(lái),避免每次訪問(wèn)都重新轉(zhuǎn)換。
此處定義tempdir目錄資源,以便保存xslt轉(zhuǎn)換的結(jié)果。
-------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceLoader參考:
FileResourceLoader
| <resource pattern="/my/virtual"> ?????????????<res-loaders:file-loader /> ??????????</resource> |
file-loader會(huì)從當(dāng)前配置文件所在的目錄中裝載
| <resource pattern="/my/virtual"> ??????????????<res-loaders:file-loader basedir="${my.basedir}" /> ??????????</resource> |
這樣,它就會(huì)從指定的basedir的子目錄中查找資源。
WebappResourceLoader:
| <resource pattern="/my/virtual"> ??????????????<res-loaders:webapp-loader /> ??????????</resource> |
從當(dāng)前WEB應(yīng)用中裝載資源,也就是從ServletContext對(duì)象中裝載資源。
ClasspathResourceLoader:
| <resource pattern="/my/virtual"> ??????????????<res-loaders:classpath-loader /> ??????????</resource> |
從classpath中裝載資源,也就是從當(dāng)前的ClassLoader對(duì)象中裝載資源。
SuperResourceLoader:
調(diào)用Resource Loading服務(wù)來(lái)取得資源。它有點(diǎn)像Java里面的super操作符。
| <resource pattern="/my/virtual"> ??????????????<res-loaders:super-loader basedir="/webroot/WEB-INF" /> ??????????</resource> |
這個(gè)操作類(lèi)似于<resource-alias>。
總結(jié)
以上是生活随笔為你收集整理的webx学习(四)——ResourceLoadingService的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 你和高级开发的距离,可能还缺这个技术框架
- 下一篇: 查阅文献时向原作者发邮件要文献的简单模板