ResourceLoader的源碼java
- public interface ResourceLoader {
-
-
- String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
-
- Resource getResource(String location);
-
- ClassLoader getClassLoader();
-
- }
咱們發現,其實ResourceLoader接口只提供了classpath前綴的支持。而classpath*的前綴支持是在它的子接口ResourcePatternResolver中。
- public interface ResourcePatternResolver extends ResourceLoader {
-
-
-
-
-
-
-
-
- String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
-
-
- Resource[] getResources(String locationPattern) throws IOException;
-
- }
經過2個接口的源碼對比,咱們發現ResourceLoader提供 classpath下單資源文件的載入,而
ResourcePatternResolver提供了多資源文件的載入。
ResourcePatternResolver有一個實現類:PathMatchingResourcePatternResolver,那咱們直奔主題,查看PathMatchingResourcePatternResolver的getResources()spring
- 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 {
-
- return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
- }
- }
- else {
-
-
- int prefixEnd = locationPattern.indexOf(":") + 1;
-
- if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
-
- return findPathMatchingResources(locationPattern);
- }
- else {
-
- return new Resource[] {getResourceLoader().getResource(locationPattern)};
- }
- }
- }
由此咱們能夠看出在加載配置文件時,以是不是以classpath*開頭分爲2大類處理場景,每大類在又根據路徑中是否包括通配符分爲2小類進行處理,
處理的流程圖以下:app
從上圖看,整個加載資源的場景有三條處理流程ide
讓咱們來看看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<URL> 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 {
-
- URL url = new URL(location);
- return new UrlResource(url);
- }
- catch (MalformedURLException ex) {
-
- return getResourceByPath(location);
- }
- }
- }
其實很簡單,若是以classpath開頭,則建立爲一個ClassPathResource,不然則試圖以URL的方式加載資源,建立一個UrlResource.
這種狀況是最複雜的,涉及到層層遞歸,那我把加了註釋的代碼發出來你們看一下,其實主要的思想就是
1.先獲取目錄,加載目錄裏面的全部資源
2.在全部資源裏面進行查找匹配,找出咱們須要的資源
- protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
-
- String rootDirPath = determineRootDir(locationPattern);
-
- String subPattern = locationPattern.substring(rootDirPath.length());
-
- Resource[] rootDirResources = getResources(rootDirPath);
- Set<Resource> result = new LinkedHashSet<Resource>(16);
-
- 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包中的資源
第1,2點比較好表理解,你們能夠自行測試,第三點表述有點繞,舉個例,如今有資源文件結構以下:
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