Resource接口
【轉】https://blog.csdn.net/hbtj_1216/article/details/85487787
參考:官方文檔
1 簡介
Java標準庫中的java.net.URL類和標準處理器對于處理低層的資源沒有提供很好的功能。例如,并沒有提供一個URL的實現能夠從classpath或者ServletContext中讀取資源等等。因此,在Spring中提供了這樣一個Resource接口,能夠更加方便的讀取各種資源。
2 Resource接口
Spring提供的Resource接口,是對第低層資源訪問進行的一個抽象,提供能方便的使用。
下面是org.springframework.core.io.Resource接口的定義:
public interface Resource extends InputStreamSource { /** * 判斷資源在物理上是否存在 */ boolean exists(); /** * 表明該資源中的非空內容是否可以通過getInputStream()讀取 */ default boolean isReadable() { return exists(); } /** * 表明該資源是否被一個打開的stream處理 */ default boolean isOpen() { return false; } /** * 判斷該資源是否代表文件系統中的一個文件 */ default boolean isFile() { return false; } /** * 返回該資源的URL */ URL getURL() throws IOException; /** * 返回該資源的URI */ URI getURI() throws IOException; /** * 返回該資源對應的File */ File getFile() throws IOException; /** * 返回一個ReadableByteChannel(可讀的字節流通道) */ default ReadableByteChannel readableChannel() throws IOException { return Channels.newChannel(getInputStream()); } /** * 返回資源中內容的長度 */ long contentLength() throws IOException; /** * 返回該資源的最后修改時間 */ long lastModified() throws IOException; /** * 創建一個和該資源相關的資源 */ Resource createRelative(String relativePath) throws IOException; /** * 返回文件名 */ @Nullable String getFilename(); /** * 返回資源的描述 */ String getDescription(); }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
Resource接口繼承的InputStreamSource接口中海油一個非常重要的方法:
public interface InputStreamSource {/** * 找到并打開資源,返回讀取資源內容的InputStream. 每次調用返回一個新的InputStream. */ InputStream getInputStream() throws IOException; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3 Spring中內置的Resource接口的實現類
Spring中包含了如下幾個Resource接口的實現類:
- UrlResource:從URL獲取資源。
- ClassPathResource:從classpath獲取資源。
- FileSystemResource:從文件系統獲取資源。
- ServletContextResource:從servlet上下文獲取資源。
- InputStreamResource:從InputStream獲取資源。
- ByteArrayResource:從字節數組獲取資源。
UrlResource
UrlResource是對java.net.URL的封裝,可以被用來訪問任何可以通過URL訪問的資源對象,例如文件、HTTP目標對象、FTP目標對象等等。
每種類型的URL都有表示該類型資源的前綴。例如file:表示訪問文件系統的URL;http:表示通過http協議訪問;ftp:表示通過ftp協議訪問。
ClassPathResource
ClassPathResource表示從classpath中獲取資源。
FileSystemResource
FileSystemResource是對java.io.File和java.nio.file.Path的封裝,代表從文件系統中讀取文件。
ServletContextResource
ServletContextResource是為了方便你從ServletContext中獲取資源而設計的,可以從相對于web應用程序的根目錄中獲取資源。
InputStreamResource
InputStreamResource是用來從給定的InputStream中獲取資源的。
ByteArrayResource
ByteArrayResource用來從給定的字節數組中獲取資源,它會創建一個ByteArrayInputStream。
4 ResourceLoader
ResourceLoader接口是用來加載資源的,它提供了一個getResource函數用來返回一個Resource對象。下面是它的定義:
public interface ResourceLoader {String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; Resource getResource(String location); @Nullable ClassLoader getClassLoader(); }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
所有的應用程序上下文都實現了ResourceLoader接口,因此可以從應用程序上下文中獲取Resource對象:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");- 1
如果在調用getResource()的時候,指定的資源路徑上沒有給出前綴,那么Spring會根據context的類型返回一個合適類型的資源對象。例如,在ClassPathXmlApplicationContext對象上調用getResource()函數,則會返回一個ClassPathResource對象。
如果你指定了前綴,那么不管context是什么類型,都將返回前綴對應的資源類型,例如:
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");- 1
下表總結了資源路徑對應的資源類型:
| classpath: | classpath:com/myapp/config.xml | 從classpath加載資源。 |
| file: | file:///data/config.xml | 從文件系統加載資源。 |
| http: | http://myserver/logo.png | 使用http協議加載資源。 |
| (none) | /data/config.xml | 根據context類型判斷。 |
5 ResourceLoaderAware接口
ResourceLoaderAware是Spring提供的一個回調接口,用于注入ResourceLoader:
public interface ResourceLoaderAware extends Aware { void setResourceLoader(ResourceLoader resourceLoader); }- 1
- 2
- 3
- 4
如果一個類實現了ResourceLoaderAware接口并在Spring上下文中注冊為一個bean,那么context會調用它的setResourceLoader()方法將context本身設置進去(因為所有的context都實現了ResourceLoader接口)。
下面是一個例子:
@Component public class ResourceBean implements ResourceLoaderAware { private ResourceLoader resourceLoader; @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public ResourceLoader getResourceLoader() { return resourceLoader; } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
上述方法采用實現ResourceLoaderAware接口的方式注入ResourceLoader,屬于侵入式的方法。從Spring 2.5之后,可以直接通過@Autowired自動注入的方式注入ResourceLoader。
6 應用個上下文和資源路徑
6.1 構造一個應用上下文
應用上下文的構造函數通常將資源的路徑以一個字符串或者字符串數組傳入。當路徑沒有前綴的時候,資源的類型依據上下文的類型構建。
下面的例子,通過資源的路徑構造一個ClassPathXmlApplicationContext上下文對象:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");- 1
下面的例子,根據資源路徑構造一個FileSystemXmlApplicationContext上下文對象:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");- 1
也可以通過路徑前綴指定具體資源的類型。
6.2 資源路徑支持通配符
可以通過*來表示一批資源文件:
/WEB-INF/*-context.xml com/mycompany/**/applicationContext.xml file:C:/some/path/*-context.xml classpath:com/mycompany/**/applicationContext.xml- 1
- 2
- 3
- 4
6.3 classpath*:和classpath:
classpath*:?和classpath:的主要區別體現在:
- classpath::只會在當前項目的WEB-INF/classes路徑下查找文件。
- classpath*::不只在當前羨慕的WEB-INF/classes路徑下查找文件,還會到第三方jar文件的WEB-INF/classes查找文件。
6.4 FileSystemResource的路徑問題
為了兼容,FileSystemXmlApplicationContext中的相對路徑和絕對路徑都將視為相對路徑,相對于當前項目的工作目錄。
下面兩個是等價的:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml");- 1
- 1
如果真的需要絕對路徑,應該盡量使用UrlResource并通過file:前綴來指定:
// actual context type doesn't matter, the Resource will always be UrlResource ctx.getResource("file:///some/resource/path/myTemplate.txt");- 1
- 2
?
轉載于:https://www.cnblogs.com/exmyth/p/11306759.html
總結
以上是生活随笔為你收集整理的Resource接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DIV背景半透明,DIV中的字不半透明
- 下一篇: 05—15