?
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
ResourceLoader
接口由能返回(或者載入)Resource
實例的對象來實現(xiàn)。
public interface ResourceLoader { Resource getResource(String location); }
所有的application context都實現(xiàn)了 ResourceLoader
接口,
因此它們可以用來獲取Resource
實例。
當你調(diào)用特定application context的 getResource()
方法,
而且資源路徑并沒有特定的前綴時,你將獲得與該application context相應(yīng)的 Resource
類型。例如:假定下面的代碼片斷是基于ClassPathXmlApplicationContext
實例上執(zhí)行的:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
這將返回ClassPathResource
;如果是基于FileSystemXmlApplicationContext
實例上執(zhí)行的,那你將獲得FileSystemResource
。而對于 WebApplicationContext
你將獲得ServletContextResource
,依此類推。
這樣你可以在特定的application context中用流行的方法載入資源。
另一方面,無論什么類型的application context,
你可以通過使用特定的前綴 classpath:
強制使用ClassPathResource
。
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
同樣的,你可以用任何標準的 java.net.URL
前綴,強制使用 UrlResource
:
Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
下面的表格概述了 String
到 Resource
的轉(zhuǎn)換規(guī)則:
表?4.1.?Resource strings
前綴 | 例子 | 說明 |
---|---|---|
classpath: |
|
從classpath中加載。 |
file: |
|
作為 |
http: |
|
作為 |
(none) |
|
根據(jù)
|
[a] 參考標題為 第?4.7.3?節(jié) “ |