日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1、spring之Resource加载

發布時間:2023/12/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1、spring之Resource加载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、對資源的抽象

Spring把其資源做了一個抽象,底層使用統一的資源訪問接口來訪問Spring的所有資源。也就是說,不管什么格式的文件,也不管文件在哪里,到Spring 底層,都只有一個訪問接口,Resource。

1.1 類結構圖

1.2 類和接口分析

1、可以看到有四個比較重要的接口 InputStreamSource、Resource、WritableResource、ContextResource。
a 、InputStreamSource接口

public interface InputStreamSource {/*** Return an {@link InputStream}.* <p>It is expected that each call creates a <i>fresh</i> stream.* <p>This requirement is particularly important when you consider an API such* as JavaMail, which needs to be able to read the stream multiple times when* creating mail attachments. For such a use case, it is <i>required</i>* that each {@code getInputStream()} call returns a fresh stream.* @return the input stream for the underlying resource (must not be {@code null})* @throws IOException if the stream could not be opened* @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)*/InputStream getInputStream() throws IOException; }

b、Resource接口
接口中定義了對于資源的判斷、對資源的獲取、對資源描述的獲取。通過該接口可以對資源進行有效的操作。但是Resource接口注重于對資源的讀取。

public interface Resource extends InputStreamSource {/*** 判斷是否存在*/boolean exists();/*** 判斷是否可讀*/boolean isReadable();/*** Return whether this resource represents a handle with an open* stream. If true, the InputStream cannot be read multiple times,* and must be read and closed to avoid resource leaks.* <p>Will be {@code false} for typical resource descriptors.* 判斷流是否可以重復讀取,如果為true的話表示不可以重復讀取,在讀取完成后需要關閉流*/boolean isOpen();/*** Return a URL handle for this resource.* @throws IOException if the resource cannot be resolved as URL,* i.e. if the resource is not available as descriptor*/URL getURL() throws IOException;/*** Return a URI handle for this resource.* @throws IOException if the resource cannot be resolved as URI,* i.e. if the resource is not available as descriptor*/URI getURI() throws IOException;/*** Return a File handle for this resource.* @throws IOException if the resource cannot be resolved as absolute* file path, i.e. if the resource is not available in a file system*/File getFile() throws IOException;/*** 資源的長度* Determine the content length for this resource.* @throws IOException if the resource cannot be resolved* (in the file system or as some other known physical resource type)*/long contentLength() throws IOException;/*** 上次更新時間* Determine the last-modified timestamp for this resource.* @throws IOException if the resource cannot be resolved* (in the file system or as some other known physical resource type)*/long lastModified() throws IOException;/*** 根據資源的當前位置,獲取相對位置的其他資源* Create a resource relative to this resource.* @param relativePath the relative path (relative to this resource)* @return the resource handle for the relative resource* @throws IOException if the relative resource cannot be determined*/Resource createRelative(String relativePath) throws IOException;/*** 返回資源的名稱* Determine a filename for this resource, i.e. typically the last* part of the path: for example, "myfile.txt".* <p>Returns {@code null} if this type of resource does not* have a filename.*/String getFilename();/*** 返回資源的描述* Return a description for this resource,* to be used for error output when working with the resource.* <p>Implementations are also encouraged to return this value* from their {@code toString} method.* @see Object#toString()*/String getDescription();}

C、WritableResource
因為Resource接口主要是注重對資源的讀取,當我們對資源進行寫入的時候,需要獲取對應的判斷和輸出流。WritableResource接口主要定義了對寫入的支持。

public interface WritableResource extends Resource {/*** 返回資源是否可以被寫入*/boolean isWritable();/*** 獲取資源的寫入流*/OutputStream getOutputStream() throws IOException;}

D、ContextResource
有些資源是相對于當前的容器的,用來獲取容器中的資源。

public interface ContextResource extends Resource {/*** Return the path within the enclosing 'context'.* <p>This is typically path relative to a context-specific root directory,* e.g. a ServletContext root or a PortletContext root.*/String getPathWithinContext();}

2、存在一個AbstractResource的抽象類,所有的對于資源獲取都繼承自AbstractResource抽象類。

3、其余的都是具體的實現類
用來加載指定的資源

二、對資源的加載

Spring框架為了更方便的獲取資源,盡量弱化程序員對各個Resource接口的實現類的感知,定義了另一個ResourceLoader接口。 接口有一個特別重要的方法:Resource getResource(String location),返回Resource實例。因此程序員在使用Spring容器時,可以不去過于計較底層Resource的實現,也不需要自己創建Resource實現類,而是直接使用ReourceLoader,獲取到bean容器本身的Resource,進而取到相關的資源信息。

2.1 類繼承圖

2.2 類和接口分析

  • 接口ResourceLoader和ResourcePatternResolver
    a、ResourceLoader接口
    只能對classpath路徑下面的資源進行加載,并且只會加載指定的文件的
  • public interface ResourceLoader {/** Pseudo URL prefix for loading from the class path: "classpath:" */String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;/*** 用來根據location來獲取對應的資源*/Resource getResource(String location);/*** 獲取類加載器*/ClassLoader getClassLoader();}

    b、ResourcePatternResolver接口
    表示會加載所有路徑下面的文件,包括jar包中的文件。同時locationPattern可以設置為表達式來加載對應的文件。

    public interface ResourcePatternResolver extends ResourceLoader {/*** 表示會加載所有路徑下面的文件,包括jar包中*/String CLASSPATH_ALL_URL_PREFIX = "classpath*:";/*** 根據*/Resource[] getResources(String locationPattern) throws IOException;}

    區別
    classpath: :表示從類路徑中加載資源,classpath:和classpath:/是等價的,都是相對于類的根路徑。資源文件庫標準的在文件系統中,也可以在JAR或ZIP的類包中。

    classpath:*:假設多個JAR包或文件系統類路徑都有一個相同的配置文件,classpath:只會在第一個加載的類路徑下查找,而classpath*:會掃描所有這些JAR包及類路徑下出現的同名文件。

  • DefaultResourceLoader
    spring實現的默認的加載器,一般其他的加載器會繼承該類,并重寫getResourceByPath方法
  • public Resource getResource(String location) {Assert.notNull(location, "Location must not be null");// 以/開頭,那么根據path去查找if (location.startsWith("/")) {return getResourceByPath(location);}// 以classpath開頭,那么抽象為ClassPathResourceelse if (location.startsWith(CLASSPATH_URL_PREFIX)) {return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());}else {try {// 其他情況采用UrlResource來進行加載URL url = new URL(location);return new UrlResource(url);}catch (MalformedURLException ex) {// No URL -> resolve as resource path.return getResourceByPath(location);}}}
  • PathMatchingResourcePatternResolver
    Spring提供了一個ResourcePatternResolver實現PathMatchingResourcePatternResolver,它是基于模式匹配的,默認使用AntPathMatcher進行路徑匹配,它除了支持ResourceLoader支持的前綴外,還額外支持“classpath*:”用于加載所有匹配的類路徑Resource,ResourceLoader不支持前綴“classpath*:”:
  • 三、Resource的一些工具類

    3.1 工具類截圖

    3.2 詳解

    1、EncodedResource
    當您使用 Resource 實現類加載文件資源時,它默認采用操作系統的編碼格式。
    如果文件資源采用了特殊的編碼格式(如 UTF-8),則在讀取資源內容時必須事先通過 EncodedResource 指定編碼格式,否則將會產生中文亂碼的問題。

    public class EncodedResource {private final Resource resource;private final String encoding;private final Charset charset;/*** 根據encoding和charset是否存在來判斷是否可以獲取Reader*/public boolean requiresReader() {return (this.encoding != null || this.charset != null);}/*** 根據EncodedResource信息獲取Reader信息*/public Reader getReader() throws IOException {if (this.charset != null) {return new InputStreamReader(this.resource.getInputStream(), this.charset);}else if (this.encoding != null) {return new InputStreamReader(this.resource.getInputStream(), this.encoding);}else {return new InputStreamReader(this.resource.getInputStream());}}/*** Open a {@code java.io.InputStream} for the specified resource, ignoring any* specified {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}.* @throws IOException if opening the InputStream failed* @see #requiresReader()* @see #getReader()*/public InputStream getInputStream() throws IOException {return this.resource.getInputStream();}}

    2、ResourcePatternUtils

    /*** Return whether the given resource location is a URL: either a* special "classpath" or "classpath*" pseudo URL or a standard URL*/public static boolean isUrl(String resourceLocation) {return (resourceLocation != null &&(resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX) ||ResourceUtils.isUrl(resourceLocation)));}/*** 根據ResourceLoader構建一個ResourcePatternResolver*/public static ResourcePatternResolver getResourcePatternResolver(ResourceLoader resourceLoader) {Assert.notNull(resourceLoader, "ResourceLoader must not be null");if (resourceLoader instanceof ResourcePatternResolver) {return (ResourcePatternResolver) resourceLoader;}else if (resourceLoader != null) {return new PathMatchingResourcePatternResolver(resourceLoader);}else {return new PathMatchingResourcePatternResolver();}}

    3、PropertiesLoaderUtils
    根據提供的Resource或者EncodedResource,將其中的內容轉換為Property內容。

    總結

    以上是生活随笔為你收集整理的1、spring之Resource加载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。