javascript
Spring5参考指南: Resources
文章目錄
- 內置Resource實現
- ResourceLoader
- ResourceLoaderAware
- 資源作為依賴
- 構造ClassPathXmlApplicationContext-快捷方式
- 資源路徑通配符
- FileSystemResource注意事項
Spring定義了Resource接口用來對資源的訪問,一般來說資源有兩種形式,一種是URL的形式從外部鏈接加載,一種是File的形式從系統本身查找。
Spring的Resource提供了如下接口:
public interface Resource extends InputStreamSource {boolean exists();boolean isOpen();URL getURL() throws IOException;File getFile() throws IOException;Resource createRelative(String relativePath) throws IOException;String getFilename();String getDescription();}Resource繼承了InputStreamSource接口,下面是其定義:
public interface InputStreamSource {InputStream getInputStream() throws IOException;}內置Resource實現
Spring有如下幾種內置資源實現:
- UrlResource
- ClassPathResource
- FileSystemResource
- ServletContextResource
- InputStreamResource
- ByteArrayResource
UrlResource
UrlResource封裝了java.net.URL,可用于訪問通常可通過url訪問的任何對象,如文件、HTTP目標、FTP目標和其他對象。所有URL可以使用一個標準化前綴來表示一個URL類型。例如:
file:用于訪問文件系統路徑。
http:用于通過HTTP協議訪問資源。
ftp:用于通過FTP訪問資源。
ClassPathResource
表示從類路徑加載資源。如果資源路徑帶上前綴ClassPath:,那么會隱式的解析為ClassPathResource。
注意,如果類資源文件是在文件系統中,則該資源實現會被解析為java.io.File, 如果是在Jar包中,則會使用java.net.URL來解析。
FileSystemResource
他是java.io.File和java.nio.file.Path的Resource實現,支持解析為File或者URL。
ServletContextResource
這是ServletContext的Resource實現,用于解釋相關Web應用程序根目錄中的相對路徑。
InputStreamResource
InputStreamResource 是InputStream 的Resource實現。只有在其他Resource實現不可用的時候才考慮使用它。
和其他的Resource實現相反,它是一個already-opened resource的描述器,所以isOpen()會返回true。 如果你想保存資源描述器或者多次讀取一個stream, 那么不要使用它。
ByteArrayResource
是byte array的Resource實現, 它創建了ByteArrayInputStream。
它對于從任何給定的字節數組加載內容都很有用,而不必求助于單次使用的InputStreamResource。
ResourceLoader
ResourceLoader用來返回Resource實例,下面是其定義:
public interface ResourceLoader {Resource getResource(String location);}所有的 application contexts 都實現了ResourceLoader類。因此所有的application contexts 都可以用來獲取Resource。
當在特定的應用程序上下文上調用getResource(),并且指定的位置路徑沒有特定的前綴時,將返回適合該特定應用程序上下文的資源類型。例如,假設對ClassPathXmlApplicationContext實例執行了以下代碼片段:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");在ClassPathXmlApplicationContext中,這個方法返回ClassPathResource,如果在FileSystemXmlApplicationContext中,方法返回FileSystemResource。 在WebApplicationContext, 方法返回ServletContextResource。 他會返回和ApplicationContext相對應的Resource實現。
當然,你可以強制ClassPathResource使用,而不管ApplicationContext到底是什么。這樣做你需要添加classpath:前綴。如下:
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");同樣的,你可以強制使用UrlResource通過添加標準的java.net.URL前綴。
Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");Resource template = ctx.getResource("https://myhost.com/resource/path/myTemplate.txt");ResourceLoaderAware
ResourceLoaderAware接口是一個特殊的回調,表明該組件需要提供一個ResourceLoader的引用。 下面是ResourceLoaderAware的定義:
public interface ResourceLoaderAware {void setResourceLoader(ResourceLoader resourceLoader); }當一個類實現了ResourceLoaderAware并被部署到application context,那么整個類就被識別為ResourceLoaderAware。 application context會去調用setResourceLoader(ResourceLoader)方法,并將其自身作為參數傳入(所有的Spring application contexts 都實現了ResourceLoader 接口)。
在應用程序組件中,你也可以使用自動裝載ResourceLoader,來替代使用ResourceLoaderAware接口。可以使用傳統的constructor或者byType的自動裝載模式。或者使用注解的方式。
資源作為依賴
如果想將靜態資源注入到Bean中,可以簡單的將String路徑轉換為Resource對象。 如果Bean定義了一個Resource類型的template屬性,那么下面就是一個很簡單的資源配置的例子:
@Data public class BeanA {private Resource template;} <bean id="myBean" class="com.flydean.beans.BeanA"><property name="template" value="bean.properties"/></bean>構造ClassPathXmlApplicationContext-快捷方式
ClassPathXmlApplicationContext提供了一個快捷方式來查找需要加載的資源路徑。
只需提供一個字符串數組,該數組只包含XML文件本身的文件名(不包含前導路徑信息),還提供一個類。然后,ClassPathXmlApplicationContext從提供的類中派生路徑信息。
如下:
public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"beanA.xml"}, BeanA.class);}下面是文件結構:
com/flydean/beans/beanA.xmlBeanA.class資源路徑通配符
Ant-style Patterns
定義資源路徑可以是用Ant-style的通配符,下面是 Ant-style patterns 的路徑例子:
/WEB-INF/*-context.xml com/mycompany/**/applicationContext.xml file:C:/some/path/*-context.xml classpath:com/mycompany/**/applicationContext.xmlclasspath:前綴*
構造基于XML的application context,路徑地址可以使用classpath*: 前綴,如下:
ApplicationContext ctx =new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");classpath* 和 classpath 有什么區別呢?
classpath* 會去查找所有匹配的classpath, 而classpath 只會找到第一個匹配的資源。
FileSystemResource注意事項
未連接到FileSystemApplicationContext的FileSystemResource(即,當FileSystemApplicationContext不是實際的ResourceLoader時)會按預期處理絕對和相對路徑。相對路徑相對于當前工作目錄,而絕對路徑相對于文件系統的根目錄。
但是,由于向后兼容性(歷史)的原因,當FileSystemApplicationContext是ResourceLoader時,這一點會發生變化。FileSystemApplicationContext強制所有附加的FileSystemResource實例將所有位置路徑視為相對路徑,不管它們是否以前導斜杠開頭。實際上,這意味著以下示例是等效的:
ApplicationContext ctx =new FileSystemXmlApplicationContext("conf/context.xml");ApplicationContext ctx =new FileSystemXmlApplicationContext("/conf/context.xml");在實踐中,如果需要真正的絕對文件系統路徑,則應避免將絕對路徑與FileSystemResource或FileSystemXmlApplicationContext一起使用,并通過使用file: URL 前綴強制使用UrlResource。以下示例說明了如何執行此操作:
// actual context type doesn't matter, the Resource will always be UrlResource ctx.getResource("file:///some/resource/path/myTemplate.txt");// force this FileSystemXmlApplicationContext to load its definition via a UrlResource ApplicationContext ctx =new FileSystemXmlApplicationContext("file:///conf/context.xml");本節的例子可參考resources
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
更多教程請參考flydean的博客
總結
以上是生活随笔為你收集整理的Spring5参考指南: Resources的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring5参考指南:事件Event
- 下一篇: Spring5参考指南: BeanWra