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