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" ????????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>表示從文件系統中裝載資源。
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。 將/myapp/conf/myfile.conf轉換成/webroot/WEB-INF/myfile.xml。 將/profiles/myname轉換成文件路徑${profile_root}/m/myname;將/profiles/othername轉換成文件路徑${profile_root}/o/othername。 |
每個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>。
總結
以上是生活随笔為你收集整理的webx学习(四)——ResourceLoadingService的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你和高级开发的距离,可能还缺这个技术框架
- 下一篇: 查阅文献时向原作者发邮件要文献的简单模板