Spring 中使用資源

2021-08-22 03:42:43 字數 898 閱讀 3345

在web應用中如果自已的程式裡面需要用到外部資源檔案(配置檔案),一般會把配置檔案放在classes下面或web-inf下面。如果載入這結檔案的話通常有兩種辦法。一是寫絕對路徑,這是最不推薦的方法。二是引入servletcontext,從相對路徑中獲得絕對路徑。寫相對路徑的時候使用servletcontext是免不了的,我的應用伺服器是tomcat,使用相對路徑找檔案都是在tomcat的bin資料夾下面找,所以我只能用servletcontext來找。後來,為了使程式能脫離servlet容器執行,就引入了spring的資源管理機制。

spring為資源管理提供了乙個resource介面,並提供了幾個resource的實現。它們都需要乙個string表示的資源的實際位置。依據這個string,spring將會自動為你選擇正確的resource實現。

下面一段**,我為了得到乙個資源的位置,定義了個屬性,乙個是file代表檔案的路徑(相對或絕對),乙個是resouce介面的屬性。在spring的任何context中都可以通過設定resource的屬性來得到資源的位置。

privatestring file;

privateresource resource;

publicvoidsetresource

(resource resource

)catch(

ioexception e) }

publicvoidsetfile

(string file

)

spring配置檔案如下:

/web-inf/config.xml

DLL中使用資源

1 在dll中使用對話方塊資源 hinstance hold afxgetresourcehandle afxsetresourcehandle getmodulehandle dll 開啟對話方塊 afxsetresourcehandle hold 2 在dll中使用activex控制項 hres...

Spring中使用QuartzJobBean定時器

1.建立class繼承quartzjobbean import org.quartz.jobexecutioncontext import org.quartz.jobexecutionexception import org.springframework.scheduling.quartz.qu...

在 pod 中使用資源

編譯完的mainbundle 和 currentbundle 檔案位置 圖中的 images.xcassets 就是日常 xcode 中的存放的位置 圖中的 qgtool.bundle 就是日常 qgtool assets的位置 在資料夾中的xib 和的層級顯示 1.在 cocoapod 使用 xi...