他这篇博客比较细的讲解了classpath与classpath*,以及通配符的使用,那些配置能成功加载到资源,那些配置加载不了资源。但是我相信仍然有很多同学不明白,为什么是这样的,知其然,不知其所以然,那么本篇文章将慢慢为你揭开神秘的面纱,让你知其然,更知其所以然。
关于spring Resource的资源类型以及继承体系我们已经在上一篇文件粗略的说了一下。Spring加载Resource文件是通过ResourceLoader来进行的,那么我们就先来看看ResourceLoader的继承体系,让我们对这个模块有一个比较系统的认知。
上图仅右边的继承体系,仅画至AbstractApplicationContext,由于ApplicationContext的继承体系,我们已经在前面章节给出,所以为了避免不必要的复杂性,本章继承体系就不引入ApplicationContext。
我们还是来关注本章的重点————classpath 与 classpath*以及通配符是怎么处理的
首先,我们来看下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();
- }
- 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是怎么处理的
我们可以看到,最关键的一句代码是:Enumeration<URL> resourceUrls = getClassLoader().getResources(path);
- 不以classpath*开头,且路径不包含通配符的
处理逻辑如下
上面我们已经贴过getResourceLoader()的逻辑了, 即默认是DefaultResourceLoader(),那我们进去看看getResouce()的实现
其实很简单,如果以classpath开头,则创建为一个ClassPathResource,否则则试图以URL的方式加载资源,创建一个UrlResource.
- 路径包含通配符的
这种情况是最复杂的,涉及到层层递归,那我把加了注释的代码发出来大家看一下,其实主要的思想就是
1.先获取目录,加载目录里面的所有资源
2.在所有资源里面进行查找匹配,找出我们需要的资源
值得注解一下的是determineRootDir()方法的作用,是确定根目录,这个根目录必须是一个能确定的路径,不会包含通配符。如果classpath*:aa/bb*/spring-*.xml,得到的将是classpath*:aa/ 可以看下他的源码
分析到这,结合测试我们可以总结一下:
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
博客原文地址: http://jinnianshilongnian.iteye.com/blog/1416322
博客原文地址: http://jinnianshilongnian.iteye.com/blog/1416322