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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中资源的加载ResourceLoader

發布時間:2023/12/10 javascript 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中资源的加载ResourceLoader 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring中資源的加載是定義在ResourceLoader接口中的,它跟前面提到的抽象資源的關系如下:

ResourceLoader的源碼

public interface ResourceLoader { /** Pseudo URL prefix for loading from the class path: "classpath:" */ String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; Resource getResource(String location); ClassLoader getClassLoader(); }

我們發現,其實ResourceLoader接口只提供了classpath前綴的支持。而classpath*的前綴支持是在它的子接口ResourcePatternResolver中。

public interface ResourcePatternResolver extends ResourceLoader { /** * Pseudo URL prefix for all matching resources from the class path: "classpath*:" * This differs from ResourceLoader's classpath URL prefix in that it * retrieves all matching resources for a given name (e.g. "/beans.xml"), * for example in the root of all deployed JAR files. * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX */ String CLASSPATH_ALL_URL_PREFIX = "classpath*:"; Resource[] getResources(String locationPattern) throws IOException; }

通過2個接口的源碼對比,我們發現ResourceLoader提供 classpath下單資源文件的載入,而ResourcePatternResolver提供了多資源文件的載入。
ResourcePatternResolver有一個實現類:PathMatchingResourcePatternResolver,那我們直奔主題,查看PathMatchingResourcePatternResolver的getResources()

public Resource[] getResources(String locationPattern) throws IOException { Assert.notNull(locationPattern, "Location pattern must not be null"); //是否以classpath*開頭 if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) { //是否包含?或者* if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) { // a class path resource pattern return findPathMatchingResources(locationPattern); } else { // all class path resources with the given name return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length())); } } else { // Only look for a pattern after a prefix here // (to not get fooled by a pattern symbol in a strange prefix). int prefixEnd = locationPattern.indexOf(":") + 1; //是否包含?或者* if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) { // a file pattern return findPathMatchingResources(locationPattern); } else { // a single resource with the given name return new Resource[] {getResourceLoader().getResource(locationPattern)}; } } }

由此我們可以看出在加載配置文件時,以是否是以classpath*開頭分為2大類處理場景,每大類在又根據路徑中是否包括通配符分為2小類進行處理,
處理的流程圖如下:


從上圖看,整個加載資源的場景有三條處理流程

以classpath*開頭,但路徑不包含通配符的
讓我們來看看findAllClassPathResources是怎么處理的

protected Resource[] findAllClassPathResources(String location) throws IOException { String path = location; if (path.startsWith("/")) { path = path.substring(1); } Enumeration<URL> resourceUrls = getClassLoader().getResources(path); Set<Resource> result = new LinkedHashSet<Resource>(16); while (resourceUrls.hasMoreElements()) { URL url = resourceUrls.nextElement(); result.add(convertClassLoaderURL(url)); } return result.toArray(new Resource[result.size()]); }

我們可以看到,最關鍵的一句代碼是:Enumeration resourceUrls = getClassLoader().getResources(path);

public ClassLoader getClassLoader() { return getResourceLoader().getClassLoader(); } public ResourceLoader getResourceLoader() { return this.resourceLoader; } //默認情況下 public PathMatchingResourcePatternResolver() { this.resourceLoader = new DefaultResourceLoader(); }

其實上面這3個方法不是最關鍵的,之所以貼出來,是讓大家清楚整個調用鏈,其實這種情況最關鍵的代碼在于ClassLoader的getResources()方法。那么我們同樣跟進去,看看源碼

public Enumeration<URL> getResources(String name) throws IOException { Enumeration[] tmp = new Enumeration[2]; if (parent != null) { tmp[0] = parent.getResources(name); } else { tmp[0] = getBootstrapResources(name); } tmp[1] = findResources(name); return new CompoundEnumeration(tmp); }

是不是一目了然了?當前類加載器,如果存在父加載器,則向上迭代獲取資源, 因此能加到jar包里面的資源文件。

不以classpath*開頭,且路徑不包含通配符的
處理邏輯如下

return new Resource[] {getResourceLoader().getResource(locationPattern)};

上面我們已經貼過getResourceLoader()的邏輯了, 即默認是DefaultResourceLoader(),那我們進去看看getResouce()的實現

public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); return new UrlResource(url); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); } } }

其實很簡單,如果以classpath開頭,則創建為一個ClassPathResource,否則則試圖以URL的方式加載資源,創建一個UrlResource.
路徑包含通配符的
這種情況是最復雜的,涉及到層層遞歸,那我把加了注釋的代碼發出來大家看一下,其實主要的思想就是
1.先獲取目錄,加載目錄里面的所有資源
2.在所有資源里面進行查找匹配,找出我們需要的資源

protected Resource[] findPathMatchingResources(String locationPattern) throws IOException { //拿到能確定的目錄,即拿到不包括通配符的能確定的路徑 比如classpath*:/aaa/bbb/spring-*.xml 則返回classpath*:/aaa/bbb/ //如果是classpath*:/aaa/*/spring-*.xml,則返回 classpath*:/aaa/ String rootDirPath = determineRootDir(locationPattern); //得到spring-*.xml String subPattern = locationPattern.substring(rootDirPath.length()); //遞歸加載所有的根目錄資源,要注意的是遞歸的時候又得考慮classpath,與classpath*的情況,而且還得考慮根路徑中是否又包含通配符,參考上面那張流程圖 Resource[] rootDirResources = getResources(rootDirPath); Set<Resource> result = new LinkedHashSet<Resource>(16); //將根目錄所有資源中所有匹配我們需要的資源(如spring-*)加載result中 for (Resource rootDirResource : rootDirResources) { rootDirResource = resolveRootDirResource(rootDirResource); if (isJarResource(rootDirResource)) { result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern)); } else if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher())); } else { result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern)); } } if (logger.isDebugEnabled()) { logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result); } return result.toArray(new Resource[result.size()]); }

值得注解一下的是determineRootDir()方法的作用,是確定根目錄,這個根目錄必須是一個能確定的路徑,不會包含通配符。如果classpath*:aa/bb*/spring-.xml,得到的將是classpath:aa/ 可以看下他的源碼

protected String determineRootDir(String location) { int prefixEnd = location.indexOf(":") + 1; int rootDirEnd = location.length(); while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) { rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1; } if (rootDirEnd == 0) { rootDirEnd = prefixEnd; } return location.substring(0, rootDirEnd); }

分析到這,結合測試我們可以總結一下:
1.無論是classpath還是classpath都可以加載整個classpath下(包括jar包里面)的資源文件。
2.classpath只會返回第一個匹配的資源,查找路徑是優先在項目中存在資源文件,再查找jar包。
3.文件名字包含通配符資源(如果spring-.xml,spring*.xml), 如果根目錄為"", classpath加載不到任何資源, 而classpath則可以加載到classpath中可以匹配的目錄中的資源,但是不能加載到jar包中的資源

classpath:notice.txt 加載不到資源
classpath*:notice*.txt 加載到resource根目錄下notice.txt
classpath:META-INF/notice*.txt 加載到META-INF下的一個資源(classpath是加載到匹配的第一個資源,就算刪除classpath下的notice.txt,他仍然可以 加載jar包中的notice.txt)
classpath:META-/notice.txt 加載不到任何資源
classpath*:META-INF/notice*.txt 加載到classpath以及所有jar包中META-INF目錄下以notice開頭的txt文件
classpath*:META-/notice.txt 只能加載到classpath下 META-INF目錄的notice.txt

總結

以上是生活随笔為你收集整理的Spring中资源的加载ResourceLoader的全部內容,希望文章能夠幫你解決所遇到的問題。

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