javascript
Spring中资源的加载ResourceLoader
Spring中資源的加載是定義在ResourceLoader接口中的,它跟前面提到的抽象資源的關系如下:
ResourceLoader的源碼
我們發現,其實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()
由此我們可以看出在加載配置文件時,以是否是以classpath*開頭分為2大類處理場景,每大類在又根據路徑中是否包括通配符分為2小類進行處理,
處理的流程圖如下:
從上圖看,整個加載資源的場景有三條處理流程
以classpath*開頭,但路徑不包含通配符的
讓我們來看看findAllClassPathResources是怎么處理的
我們可以看到,最關鍵的一句代碼是: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*開頭,且路徑不包含通配符的
處理邏輯如下
上面我們已經貼過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.在所有資源里面進行查找匹配,找出我們需要的資源
值得注解一下的是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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 伺服怎么接单相220伏_乐利网带你认识伺
- 下一篇: 西南科技大学OJ题 循环队列0965