Spring加载classpath与classpath*的过程剖析

16 篇文章 0 订阅

好记忆不如烂笔头 ,能记点什么,就记点什么吧、


原文地址:http://blog.csdn.net/zl3450341/article/details/9306983


Spring加载Resource文件是通过ResourceLoader来进行的,那么我们就先来看看ResourceLoader的继承体系,让我们对这个模块有一个比较系统的认知。


上图仅右边的继承体系,仅画至AbstractApplicationContext,由于ApplicationContext的继承体系,我们已经在前面章节给出,所以为了避免不必要的复杂性,本章继承体系就不引入ApplicationContext。

  



我们还是来关注本章的重点————classpath 与 classpath*以及通配符是怎么处理的


首先,我们来看下ResourceLoader的源码

[java]  view plain  copy
  1. public interface ResourceLoader {  
  2.   
  3.     /** Pseudo URL prefix for loading from the class path: "classpath:" */  
  4.     String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;  
  5.       
  6.     Resource getResource(String location);  
  7.   
  8.     ClassLoader getClassLoader();  
  9.   
  10. }  

我们发现,其实ResourceLoader接口只提供了classpath前缀的支持。而classpath*的前缀支持是在它的子接口ResourcePatternResolver中。

[java]  view plain  copy
  1. public interface ResourcePatternResolver extends ResourceLoader {  
  2.   
  3.     /** 
  4.      * Pseudo URL prefix for all matching resources from the class path: "classpath*:" 
  5.      * This differs from ResourceLoader's classpath URL prefix in that it 
  6.      * retrieves all matching resources for a given name (e.g. "/beans.xml"), 
  7.      * for example in the root of all deployed JAR files. 
  8.      * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX 
  9.      */  
  10.     String CLASSPATH_ALL_URL_PREFIX = "classpath*:";  
  11.   
  12.       
  13.     Resource[] getResources(String locationPattern) throws IOException;  
  14.   
  15. }  

   通过2个接口的源码对比,我们发现ResourceLoader提供 classpath下单资源文件的载入,而 ResourcePatternResolver提供了多资源文件的载入。

  ResourcePatternResolver有一个实现类:PathMatchingResourcePatternResolver,那我们直奔主题,查看PathMatchingResourcePatternResolver的getResources()

[java]  view plain  copy
  1. public Resource[] getResources(String locationPattern) throws IOException {  
  2.         Assert.notNull(locationPattern, "Location pattern must not be null");  
  3.         //是否以classpath*开头  
  4.         if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {  
  5.             //是否包含?或者*  
  6.             if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {  
  7.                 // a class path resource pattern  
  8.                 return findPathMatchingResources(locationPattern);  
  9.             }  
  10.             else {  
  11.                 // all class path resources with the given name  
  12.                 return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));  
  13.             }  
  14.         }  
  15.         else {  
  16.             // Only look for a pattern after a prefix here  
  17.             // (to not get fooled by a pattern symbol in a strange prefix).  
  18.             int prefixEnd = locationPattern.indexOf(":") + 1;  
  19.             //是否包含?或者*  
  20.             if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {  
  21.                 // a file pattern  
  22.                 return findPathMatchingResources(locationPattern);  
  23.             }  
  24.             else {  
  25.                 // a single resource with the given name  
  26.                 return new Resource[] {getResourceLoader().getResource(locationPattern)};  
  27.             }  
  28.         }  
  29.     }  

由此我们可以看出在加载配置文件时,以是否是以classpath*开头分为2大类处理场景,每大类在又根据路径中是否包括通配符分为2小类进行处理,

处理的流程图如下:


从上图看,整个加载资源的场景有三条处理流程

  • 以classpath*开头,但路径不包含通配符的
             让我们来看看findAllClassPathResources是怎么处理的
[java]  view plain  copy
  1. protected Resource[] findAllClassPathResources(String location) throws IOException {  
  2.     String path = location;  
  3.     if (path.startsWith("/")) {  
  4.         path = path.substring(1);  
  5.     }  
  6.     Enumeration<URL> resourceUrls = getClassLoader().getResources(path);  
  7.     Set<Resource> result = new LinkedHashSet<Resource>(16);  
  8.     while (resourceUrls.hasMoreElements()) {  
  9.         URL url = resourceUrls.nextElement();  
  10.         result.add(convertClassLoaderURL(url));  
  11.     }  
  12.     return result.toArray(new Resource[result.size()]);  
  13. }  

    我们可以看到,最关键的一句代码是:Enumeration<URL> resourceUrls = getClassLoader().getResources(path); 
[java]  view plain  copy
  1.     public ClassLoader getClassLoader() {  
  2.         return getResourceLoader().getClassLoader();  
  3.     }  
  4.   
  5.   
  6. public ResourceLoader getResourceLoader() {  
  7.         return this.resourceLoader;  
  8.     }  
  9.   
  10. //默认情况下  
  11. public PathMatchingResourcePatternResolver() {  
  12.         this.resourceLoader = new DefaultResourceLoader();  
  13.     }  
其实上面这3个方法不是最关键的,之所以贴出来,是让大家清楚整个调用链,其实这种情况最关键的代码在于ClassLoader的getResources()方法。那么我们同样跟进去,看看源码
[java]  view plain  copy
  1. public Enumeration<URL> getResources(String name) throws IOException {  
  2. Enumeration[] tmp = new Enumeration[2];  
  3. if (parent != null) {  
  4.     tmp[0] = parent.getResources(name);  
  5. else {  
  6.     tmp[0] = getBootstrapResources(name);  
  7. }  
  8. tmp[1] = findResources(name);  
  9.   
  10. return new CompoundEnumeration(tmp);  
  11.    }  
是不是一目了然了?当前类加载器,如果存在父加载器,则向上迭代获取资源, 因此能加到jar包里面的资源文件。

  • 不以classpath*开头,且路径不包含通配符的
处理逻辑如下           
[java]  view plain  copy
  1. return new Resource[] {getResourceLoader().getResource(locationPattern)};  
上面我们已经贴过getResourceLoader()的逻辑了, 即默认是DefaultResourceLoader(),那我们进去看看getResouce()的实现
[java]  view plain  copy
  1. public Resource getResource(String location) {  
  2.     Assert.notNull(location, "Location must not be null");  
  3.     if (location.startsWith(CLASSPATH_URL_PREFIX)) {  
  4.         return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());  
  5.     }  
  6.     else {  
  7.         try {  
  8.             // Try to parse the location as a URL...  
  9.             URL url = new URL(location);  
  10.             return new UrlResource(url);  
  11.         }  
  12.         catch (MalformedURLException ex) {  
  13.             // No URL -> resolve as resource path.  
  14.             return getResourceByPath(location);  
  15.         }  
  16.     }  
  17. }  

其实很简单,如果以classpath开头,则创建为一个ClassPathResource,否则则试图以URL的方式加载资源,创建一个UrlResource.
  • 路径包含通配符的
             这种情况是最复杂的,涉及到层层递归,那我把加了注释的代码发出来大家看一下,其实主要的思想就是
1.先获取目录,加载目录里面的所有资源
2.在所有资源里面进行查找匹配,找出我们需要的资源
[java]  view plain  copy
  1. protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {  
  2.         //拿到能确定的目录,即拿到不包括通配符的能确定的路径  比如classpath*:/aaa/bbb/spring-*.xml 则返回classpath*:/aaa/bbb/                                     //如果是classpath*:/aaa/*/spring-*.xml,则返回 classpath*:/aaa/  
  3.         String rootDirPath = determineRootDir(locationPattern);  
  4.         //得到spring-*.xml  
  5.         String subPattern = locationPattern.substring(rootDirPath.length());  
  6.         //递归加载所有的根目录资源,要注意的是递归的时候又得考虑classpath,与classpath*的情况,而且还得考虑根路径中是否又包含通配符,参考上面那张流程图  
  7.         Resource[] rootDirResources = getResources(rootDirPath);  
  8.         Set<Resource> result = new LinkedHashSet<Resource>(16);  
  9.         //将根目录所有资源中所有匹配我们需要的资源(如spring-*)加载result中  
  10.         for (Resource rootDirResource : rootDirResources) {  
  11.             rootDirResource = resolveRootDirResource(rootDirResource);  
  12.             if (isJarResource(rootDirResource)) {  
  13.                 result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));  
  14.             }  
  15.             else if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {  
  16.                 result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));  
  17.             }  
  18.             else {  
  19.                 result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));  
  20.             }  
  21.         }  
  22.         if (logger.isDebugEnabled()) {  
  23.             logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);  
  24.         }  
  25.         return result.toArray(new Resource[result.size()]);  
  26.     }  

值得注解一下的是determineRootDir()方法的作用,是确定根目录,这个根目录必须是一个能确定的路径,不会包含通配符。如果classpath*:aa/bb*/spring-*.xml,得到的将是classpath*:aa/  可以看下他的源码

[java]  view plain  copy
  1. protected String determineRootDir(String location) {  
  2.     int prefixEnd = location.indexOf(":") + 1;  
  3.     int rootDirEnd = location.length();  
  4.     while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) {  
  5.         rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1;  
  6.     }  
  7.     if (rootDirEnd == 0) {  
  8.         rootDirEnd = prefixEnd;  
  9.     }  
  10.     return location.substring(0, rootDirEnd);  
  11. }  




分析到这,结合测试我们可以总结一下:
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

大家感觉一下吧

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值