欧美日韩午夜精品不卡综合-欧美日韩系列-欧美日韩小视频-欧美日韩性-成人五月网-成人五级毛片免费播放

二維碼
企資網(wǎng)

掃一掃關(guān)注

當(dāng)前位置: 首頁(yè) » 企業(yè)資訊 » 產(chǎn)業(yè) » 正文

_Resource_資源加載

放大字體  縮小字體 發(fā)布日期:2021-12-29 03:05:38    作者:馮園玲    瀏覽次數(shù):65
導(dǎo)讀

Resource 是 Spring 對(duì)資源得統(tǒng)一封裝接口,實(shí)現(xiàn)對(duì)各類資源得統(tǒng)一處理。Resource 接口聲明了一些訪問(wèn)資源得能力,public interface Resource extends InputStreamSource {// 判斷資源是否存在boolean exists();// 判

Resource 是 Spring 對(duì)資源得統(tǒng)一封裝接口,實(shí)現(xiàn)對(duì)各類資源得統(tǒng)一處理。

Resource 接口聲明了一些訪問(wèn)資源得能力,

public interface Resource extends InputStreamSource { // 判斷資源是否存在 boolean exists(); // 判斷資源是否可讀,如果為 true,其內(nèi)容未必真得可讀,為 false,則一定不可讀 default boolean isReadable() { return exists(); } // 判斷資源是否已經(jīng)打開(kāi),主要針對(duì)流類型資源(InputStreamResource), default boolean isOpen() { return false; } // 判斷資源是否是文件 default boolean isFile() { return false; } // 獲取資源得 URL 地址,如果資源不能解析為 URL,則拋出異常 URL getURL() throws IOException; // 獲取資源得 URI 地址,如果資源不能解析為 URI,則拋出異常 URI getURI() throws IOException; // 獲取資源文件,如果資源不是文件,則拋出異常 File getFile() throws IOException; // NIO default ReadableByteChannel readableChannel() throws IOException { return Channels.newChannel(getInputStream()); } // 獲取資源內(nèi)容得長(zhǎng)度 long contentLength() throws IOException; // 獲取資源蕞后更新時(shí)間 long lastModified() throws IOException; // 根據(jù)資源相對(duì)路徑創(chuàng)建新資源 Resource createRelative(String relativePath) throws IOException; // 獲取資源文件名 String getFilename(); // 獲取資源描述,通常是資源全路徑(實(shí)際文件名或者URL地址) String getDescription();}

Resource 接口繼承了 InputStreamSource 接口,這個(gè)接口只聲明了一個(gè)方法,就是獲取資源得 IO 流。

public interface InputStreamSource { // 獲取資源輸入IO流 InputStream getInputStream() throws IOException;}

Resource 擁有眾多得實(shí)現(xiàn)類,不同得實(shí)現(xiàn)類代表著不同得資源。接下來(lái)學(xué)習(xí)幾個(gè)常用得實(shí)現(xiàn)類得使用方法。

實(shí)現(xiàn)類

描述

ClassPathResource

通過(guò)類路徑獲取資源

FileSystemResource

通過(guò)文件系統(tǒng)獲取資源

UrlResource

通過(guò) URL 地址獲取遠(yuǎn)程資源

ServletContextResource

獲取 ServletContext 環(huán)境下得資源

ByteArrayResource

獲取字節(jié)數(shù)組封裝得資源

InputStreamResource

獲取輸入流封裝得資源

通過(guò) Resource 加載資源

ClassPathResource

如果資源在項(xiàng)目?jī)?nèi),可以通過(guò)類路徑讀取資源,主要通過(guò)如下兩種方式

  • Class.getResourceAsStream(path)
  • path 以 / 開(kāi)頭,表示可能嗎?路徑,從 classpath 根目錄開(kāi)始查找資源
  • path 不以 / 開(kāi)頭,表示相對(duì)路徑,從 class 文件目錄開(kāi)始查找資源
  • ClassLoader.getResourceAsStream(path)
  • path 都不以 / 開(kāi)頭,從 classpath 根目錄開(kāi)始查找資源

    ClassPathResource 其實(shí)就是對(duì)以上兩種方式進(jìn)行了封裝,查看源碼,就可以知道

    public class ClassPathResource extends AbstractFileResolvingResource { private final String path; private ClassLoader classLoader; private Class<?> clazz; public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) { is = this.clazz.getResourceAsStream(this.path); } else if (this.classLoader != null) { is = this.classLoader.getResourceAsStream(this.path); } else { is = ClassLoader.getSystemResourceAsStream(this.path); } if (is == null) { throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); } return is; }}

    ClassPathResource 得使用方式如下所示

    public class ClassPathResourceTest { public static void main(String[] args) throws Exception { // 只傳 path,相當(dāng)于使用默認(rèn)得 ClassLoader 進(jìn)行加載 Resource resource1 = new ClassPathResource("com/test/hello.md"); System.out.println("resource1:" + resource1.getInputStream()); // path 前面加 "/",會(huì)自動(dòng)去掉,與不加 "/" 是一樣得效果 Resource resource2 = new ClassPathResource("/com/test/hello.md"); System.out.println("resource2:" + resource2.getInputStream()); // 使用 Class 從 classpath 進(jìn)行加載,path 前面加 "/" 與不加效果一樣 Resource resource3 = new ClassPathResource("/com/test/hello.md", ClassPathResourceTest.class); System.out.println("resource3:" + resource3.getInputStream()); // 使用 Class 得相對(duì)路徑進(jìn)行加載 Resource resource4 = new ClassPathResource("../hello.md", ClassPathResourceTest.class); System.out.println("resource4:" + resource4.getInputStream()); // 使用指定得 ClassLoader 進(jìn)行加載,從 classpath 根目錄進(jìn)行加載 Resource resource5 = new ClassPathResource("com/test/hello.md", ClassPathResourceTest.class.getClassLoader()); System.out.println("resource5:" + resource5.getInputStream()); }}

    FileSystemResource

    如果資源本地文件系統(tǒng),可以通過(guò)文件路徑讀取資源

    public class FileSystemResourceTest { public static void main(String[] args) throws Exception { // 使用文件路徑進(jìn)行加載 Resource resource1 = new FileSystemResource("d:\\test.txt"); System.out.println("resource1:" + resource1.getInputStream()); // 使用 File 進(jìn)行加載 Resource resource2 = new FileSystemResource(new File("d:\\test.txt")); System.out.println("resource2:" + resource2.getInputStream()); }}

    查看源碼,可以知道 FileSystemResource 是基于 java.nio.file.Path 實(shí)現(xiàn)。

    public class FileSystemResource extends AbstractResource implements WritableResource { private final String path; private final File file; private final Path filePath; public InputStream getInputStream() throws IOException { try { return Files.newInputStream(this.filePath); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } }}

    UrlResource

    如果資源在遠(yuǎn)程服務(wù)器,則只能通過(guò) URL 地址進(jìn)行獲取。

    public class FileSystemResourceTest { public static void main(String[] args) throws Exception { // 使用 Http 協(xié)議得 URL 地址進(jìn)行加載 Resource resource1 = new UrlResource("docs.spring.io/spring/docs/4.0.0.M1/spring-framework-reference/pdf/spring-framework-reference.pdf"); System.out.println("resource1:" + resource1.getInputStream()); // 使用 file 訪問(wèn)本地文件系統(tǒng) Resource resource2 = new UrlResource("file:d:\\test.txt"); System.out.println("resource2:" + resource2.getInputStream()); }}

    查看源碼中得實(shí)現(xiàn)

    public class UrlResource extends AbstractFileResolvingResource { private final URI uri; private final URL url; private volatile URL cleanedUrl; public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); ResourceUtils.useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }}

    ByteArrayResource

    資源即可以是文件,也可以是解析后得數(shù)據(jù)

    public class ByteArrayResourceTest { public static void main(String[] args) throws Exception { ByteArrayResource resource1 = new ByteArrayResource("Hello".getBytes()); System.out.println("resource1:" + resource1.getInputStream()); }}

    查看源碼,可以看到 getInputStream() 方法每次都會(huì)組裝一個(gè)全新得 ByteArrayInputStream 流

    public class ByteArrayResource extends AbstractResource { private final byte[] byteArray; private final String description; public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(this.byteArray); }}

    InputStreamResource

    使用 Stream 得 Resource,通過(guò) getInputStream 方法進(jìn)行資源加載,但是只能加載一次。

    public class InputStreamResourceTest { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("d:\\test.txt"); InputStreamResource resource1 = new InputStreamResource(is); System.out.println("resource1:" + resource1.getInputStream()); is.close(); }}

    構(gòu)造方法傳入得就是 Stream,查看源碼,對(duì) Stream 得使用進(jìn)行控制。

    public class InputStreamResource extends AbstractResource { private final InputStream inputStream; private final String description; private boolean read = false; public InputStream getInputStream() throws IOException, IllegalStateException { if (this.read) { throw new IllegalStateException("InputStream has already been read - " + "do not use InputStreamResource if a stream needs to be read multiple times"); } this.read = true; return this.inputStream; }}通過(guò) ResourceLoader 加載資源

    Resource 雖然統(tǒng)一了各類資源得加載方式,但實(shí)現(xiàn)類眾多,為了更方便地使用 Resource,Spring 提供了 ResourceLoader 接口,專門用來(lái)加載 Resource。

    public interface ResourceLoader {Resource getResource(String location);ClassLoader getClassLoader();}

    ResourceLoader 得使用

    public class ResourceLoaderTest { public static void main(String[] args) throws Exception { ResourceLoader loader = new DefaultResourceLoader(); Resource resource1 = loader.getResource("特別baidu"); System.out.println("resource1 -- " + resource1.getClass().getSimpleName() + " -- " + resource1.getInputStream()); Resource resource2 = loader.getResource("classpath:com/test/hello3.md"); System.out.println("resource2 -- " + resource2.getClass().getSimpleName() + " -- " + resource2.getInputStream()); Resource resource3 = loader.getResource("com/test/hello3.md"); System.out.println("resource3 -- " + resource3.getClass().getSimpleName() + " -- " + resource3.getInputStream()); Resource resource4 = loader.getResource("file://d:\\test.txt"); System.out.println("resource4 -- " + resource4.getClass().getSimpleName() + " -- " + resource4.getInputStream()); }}

    輸出如下,

    resource1 -- UrlResource -- sun.特別protocol.http.HttpURLConnection$HttpInputStream等61e717c2resource2 -- ClassPathResource -- java.io.BufferedInputStream等3b764bceresource3 -- ClassPathContextResource -- java.io.BufferedInputStream等4c98385cresource4 -- FileUrlResource -- java.io.BufferedInputStream等73a8dfcc

    查看源碼,可以清楚看到在 DefaultResourceLoader 中對(duì) location 得處理邏輯。

    public class DefaultResourceLoader implements ResourceLoader { private final Set<ProtocolResolver> protocolResolvers = new linkedHashSet<>(4); public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); // 使用 protocolResolvers 進(jìn)行分析,但上例中并沒(méi)有設(shè)置,跳過(guò) for (ProtocolResolver protocolResolver : getProtocolResolvers()) { Resource resource = protocolResolver.resolve(location, this); if (resource != null) { return resource; } } // 判斷是否 "/" 開(kāi)頭,是則返回 ClassPathContextResource if (location.startsWith("/")) { return getResourceByPath(location); } // 判斷是否 "classpath" 開(kāi)頭,是則返回 ClassPathResource else if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // 如果都不是,則使用 URL 進(jìn)行獲取 URL url = new URL(location); // 如果是系統(tǒng)文件,則返回 FileUrlResource,否則返回 UrlResource return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url)); } catch (MalformedURLException ex) { // 默認(rèn)返回 ClassPathContextResource return getResourceByPath(location); } } } protected Resource getResourceByPath(String path) { return new ClassPathContextResource(path, getClassLoader()); }}

    DefaultResourceLoader 只是 ResourceLoader 得一個(gè)默認(rèn)實(shí)現(xiàn),ResourceLoader 還有一個(gè)繼承接口 ResourcePatternResolver,這個(gè)接口提供了基于 Ant 風(fēng)格得通配符解析路徑得能力。

    public interface ResourcePatternResolver extends ResourceLoader {String CLASSPATH_ALL_URL_PREFIX = "classpath*:";Resource[] getResources(String locationPattern) throws IOException;}

    而 ApplicationContext 接口繼承了 ResourcePatternResolver 接口,所以,所有得 SpringContext 都可能通過(guò) Ant 通配符解析加載資源。

    public class ApplicationContextTest { public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.refresh(); Resource[] resources = context.getResources("classpath:com/testa.xml

    匹配 example 目錄及其子目錄下得 a.xml 文件

    查看源碼,看看 Spring 是如何實(shí)現(xiàn)支持 Ant 通配符解析得。

    getResources 得實(shí)現(xiàn)在 GenericApplicationContext 類中。GenericApplicationContext 類中有一個(gè) ResourceLoader 成員變量,可以進(jìn)行自定義設(shè)置,所以 GenericApplicationContext 使用得是組合得方式。

    public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry { private ResourceLoader resourceLoader; public Resource[] getResources(String locationPattern) throws IOException { if (this.resourceLoader instanceof ResourcePatternResolver) { return ((ResourcePatternResolver) this.resourceLoader).getResources(locationPattern); } return super.getResources(locationPattern); }}

    如果 ResourceLoader 沒(méi)有設(shè)置,或者設(shè)置得不是 ResourcePatternResolver 得實(shí)現(xiàn)類,那么調(diào)用父類得 getResources 方法,也就是 AbstractApplicationContext 中得實(shí)現(xiàn)。

    AbstractApplicationContext 構(gòu)造方法中創(chuàng)建了一個(gè)默認(rèn)得 PathMatchingResourcePatternResolver 對(duì)象,調(diào)用 getResources 方法進(jìn)行資源加載時(shí),則使用這個(gè)對(duì)象進(jìn)行加載。另外需要注意得是,AbstractApplicationContext 也繼承了 DefaultResourceLoader 類,當(dāng)調(diào)用 getResource 方法進(jìn)行資源加載時(shí),則是調(diào)用得 DefaultResourceLoader 中得實(shí)現(xiàn)。

    public abstract class AbstractApplicationContext extends DefaultResourceLoaderimplements ConfigurableApplicationContext { private ResourcePatternResolver resourcePatternResolver; public AbstractApplicationContext() { this.resourcePatternResolver = getResourcePatternResolver(); } protected ResourcePatternResolver getResourcePatternResolver() { return new PathMatchingResourcePatternResolver(this); } public Resource[] getResources(String locationPattern) throws IOException { return this.resourcePatternResolver.getResources(locationPattern); } }

    在 PathMatchingResourcePatternResolver 類中,getResource 方法得實(shí)現(xiàn)是回調(diào) resourceLoader 得該方法,resourceLoader 初始化得是 AbstractApplicationContext 得實(shí)例,所以實(shí)際調(diào)用得還是 DefaultResourceLoader 中得實(shí)現(xiàn)。getResources 方法中,對(duì)通配符得解析都在 findPathMatchingResources 方法中。解析得過(guò)程也不算復(fù)雜,就是先獲取通配符之前得目錄,然后通過(guò)文件系統(tǒng),一層層地輪詢匹配,得到所有得文件,再組裝成 FileSystemResource 對(duì)象。

    public class PathMatchingResourcePatternResolver implements ResourcePatternResolver { private final ResourceLoader resourceLoader; private PathMatcher pathMatcher = new AntPathMatcher(); // 調(diào)用 ResourceLoader 得 getResource 方法,其實(shí)就是調(diào)用得 DefaultResourceLoader 類中得方法 public Resource getResource(String location) { return getResourceLoader().getResource(location); } public Resource[] getResources(String locationPattern) throws IOException { Assert.notNull(locationPattern, "Location pattern must not be null"); if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) { if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) { // 如果路徑中有通配符,則解析通配符 return findPathMatchingResources(locationPattern); } else { // 如果路徑中沒(méi)有通配符,直接加載即可 return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length())); } } else { int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 : locationPattern.indexOf(':') + 1); if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) { // 如果路徑中有通配符,則解析通配符 return findPathMatchingResources(locationPattern); } else { return new Resource[] {getResourceLoader().getResource(locationPattern)}; } } } protected Resource[] findPathMatchingResources(String locationPattern) throws IOException { // 獲取通配符之前得目錄 String rootDirPath = determineRootDir(locationPattern); String subPattern = locationPattern.substring(rootDirPath.length()); // 再調(diào) getResources 進(jìn)行加載,rootDirPath 一定是沒(méi)有通配符得 Resource[] rootDirResources = getResources(rootDirPath); Set<Resource> result = new linkedHashSet<>(16); for (Resource rootDirResource : rootDirResources) { rootDirResource = resolveRootDirResource(rootDirResource); URL rootDirUrl = rootDirResource.getURL(); if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { // vfs 開(kāi)頭 result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher())); } else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) { // Jar 包目錄 result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirUrl, subPattern)); } else { // result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern)); } } return result.toArray(new Resource[0]); } protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { File rootDir; try { rootDir = rootDirResource.getFile().getAbsoluteFile(); } catch (FileNotFoundException ex) { return Collections.emptySet(); } catch (Exception ex) { return Collections.emptySet(); } return doFindMatchingFileSystemResources(rootDir, subPattern); } protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException { // 枚舉目錄下所有得文件,并與 subPattern 進(jìn)行匹配 Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern); Set<Resource> result = new linkedHashSet<>(matchingFiles.size()); for (File file : matchingFiles) { result.add(new FileSystemResource(file)); } return result; } }蕞后一下,共同學(xué)習(xí) Spring 框架源碼

  •  
    (文/馮園玲)
    免責(zé)聲明
    本文僅代表作發(fā)布者:馮園玲個(gè)人觀點(diǎn),本站未對(duì)其內(nèi)容進(jìn)行核實(shí),請(qǐng)讀者僅做參考,如若文中涉及有違公德、觸犯法律的內(nèi)容,一經(jīng)發(fā)現(xiàn),立即刪除,需自行承擔(dān)相應(yīng)責(zé)任。涉及到版權(quán)或其他問(wèn)題,請(qǐng)及時(shí)聯(lián)系我們刪除處理郵件:[email protected]。
     

    微信

    關(guān)注
    微信

    微信二維碼

    WAP二維碼

    客服

    聯(lián)系
    客服

    聯(lián)系客服:

    在線QQ: 303377504

    客服電話: 020-82301567

    E_mail郵箱: [email protected]

    微信公眾號(hào): weishitui

    客服001 客服002 客服003

    工作時(shí)間:

    周一至周五: 09:00 - 18:00

    反饋

    用戶
    反饋

    主站蜘蛛池模板: 久久中文亚洲国产 | 国产免费自拍 | 久久久这里只有精品加勒比 | 欧美亚洲日本国产综合网 | 91久久青青草原免费 | 国产成人免费高清视频网址 | 性久久久久久久久 | 久久久久亚洲精品中文字幕 | 久久九九免费 | 成人99国产精品一级毛片 | 国产精品成人久久久久 | 欧美一级特黄乱妇高清视频 | 国产成人精品在线观看 | 成人公开视频 | 日产国产精品久久久久久 | 欧美成人午夜免费完成 | 欧美精品免费线视频观看视频 | 日本高清毛片视频在线看 | 波多野结衣一区在线 | 久久久黄色大片 | 99国产高清久久久久久网站 | 国产精品v欧美精品v日本精 | 日本一二线不卡在线观看 | 亚洲欧美一区二区三区国产精品 | a级毛片在线播放 | 欧美aⅴ在线 | 看久久久久毛片婷婷色 | 国产精品久久久久久福利 | 欧美亚洲国产精品久久 | 久久久久久久久久久96av | 欧美透逼 | 久久精品国产免费看久久精品 | 国产成人精品视频一区二区不卡 | 免费一级特黄欧美大片久久网 | 亚洲国产在 | 美女黄视频网站 | 日本肥老妇色xxxxx日本老妇 | 久久精品中文字幕有码日本 | 日本乱人伦片中文三区 | 免费观看成人久久网免费观看 | 国产欧美一区二区三区视频在线观看 |