天天看点

Spring4.3.x 浅析xml配置的解析过程(2)——使用ResourceLoader创建Resource对象

概述

在走进XmlBeanDefinitionReader中已经讲过XmlBeanDefinitionReader把xml配置信息转换成一个一个的BeanDefinition对象的大致过程,在这个过程中还有几个细节没有讲到,这一篇,就来探讨其中一个细节——ResourceLoader如何根据指定的location生成Resource对象。

下面我们从XmlBeanDefinitionReader 使用xml配置文件地址加载BeanDefinition对象的入口loadBeanDefinitions(String location)方法开始。loadBeanDefinitions方法是XmlBeanDefinitionReader继承自其父类AbstractBeanDefinitionReader。下面代码是loadBeanDefinitions方法在AbstractBeanDefinitionReader类中的实现。

public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
        return loadBeanDefinitions(location, null);
    }
           
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
        ResourceLoader resourceLoader = getResourceLoader();
        if (resourceLoader == null) {
            throw new BeanDefinitionStoreException(
                    "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
        }

        if (resourceLoader instanceof ResourcePatternResolver) {
            // 使用资源模式解析器解析配置文件的路径并加载资源
            try {
                // 加载所有与指定location参数匹配的所有资源
                Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
                // 加载指定的资源中的所有BeanDefinition
                int loadCount = loadBeanDefinitions(resources);
                if (actualResources != null) {
                    for (Resource resource : resources) {
                        actualResources.add(resource);
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
                }
                return loadCount;
            } catch (IOException ex) {
                throw new BeanDefinitionStoreException(
                        "Could not resolve bean definition resource pattern [" + location + "]", ex);
            }
        } else {
            // 直接加载资源且只加载一个资源,默认使用DefaultResourceLoader的getResource方法
            Resource resource = resourceLoader.getResource(location);
            // 加载指定的resource中的所有BeanDefinition
            int loadCount = loadBeanDefinitions(resource);

            if (actualResources != null) {
                actualResources.add(resource);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
            }
            return loadCount;
        }
    }   
           

Spring提供了ResourceLoader接口用于加载不同的Resource对象,即将不同Resource对象的创建交给ResourceLoader来完成。Spring通过两种方式加载资源,一种是根据具体的路径加载一个资源,另一种方式通过模式匹配来加载多个资源。前者通过ResourceLoader的getResource(String location)方法实现,后者通过ResourceLoader子接口ResourcePatternResolver扩展的getResources(String locationPattern)方法实现。

上面的loadBeanDefinitions(String location, Set<Resource> actualResources)方法首先判断当前持有的ResourceLoader的类型,如果实现了ResourcePatternResolver接口,则通过第二种方式加载资源,否则使用第一种方式加载一个资源, 下面通过spring源码的分析来探讨这两个方法。

1 根据具体的路径加载资源

在Spring中加载单个资源有个默认的实现,那就是DefaultResourceLoader的getResource(String location)方法,而XmlWebApplicationContext继承了此方法,getResource方法的代码如下。

/**
    * 根据指定location获取第一个查找到的资源
    */
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");
        // 声明有:String CLASSPATH_URL_PREFIX ="classpath:";
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            // 返回ClassPathResource对象
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
        } else {
            try {
                // 把location解析成URL对象
                URL url = new URL(location);
                // 返回UrlResource对象
                return new UrlResource(url);
            } catch (MalformedURLException ex) {
                // 给定的location是一个相对地址,即没有前缀。
                // ->把location解析为一个ClassPathContextResource
                return getResourceByPath(location);
            }
        }
    }
           
@Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");

        // 首先使用ProtocolResolver来通过location参数创建Resource对象
        // spring4.3开始才有的
        for (ProtocolResolver protocolResolver : this.protocolResolvers) {
            Resource resource = protocolResolver.resolve(location, this);
            if (resource != null) {
                return resource;
            }
        }

        if (location.startsWith("/")) {
            return getResourceByPath(location);
        } else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            // 声明有:String CLASSPATH_URL_PREFIX ="classpath:";
            // 返回ClassPathResource对象
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
        } else {
            try {
                // 把location解析成URL对象
                URL url = new URL(location);
                // 返回UrlResource对象
                return new UrlResource(url);
            }
            catch (MalformedURLException ex) {
                // 给定的location是一个相对地址,即没有前缀。
                // ->默认把location解析为一个ClassPathContextResource
                return getResourceByPath(location);
            }
        }
    }
           

这段代码处理三种类型的location:

第一种是以classpath:为前缀的,这种location参数直接返回一个ClassPathResource对象,表示加载classes路径下的资源;

第二种是使用网络协议作为前缀的,比如http、ftp等,这种直接返回一个UrlResource对象;

第三种是无前缀的,在默认实现中和第一种一样是加载classes路径下的资源,只是现在返回的对象是ClassPathContextResource对象,代码如下。

/**
    * 根据指定路径获取资源。
    * 返回的是一个ClassPathContextResource对象
    */
    protected Resource getResourceByPath(String path) {
        return new ClassPathContextResource(path, getClassLoader());
    }

    /**
    * ClassPathContextResource 通过实现ContextResource,明确的指明了加载的文件是相对于上线文所在的路径。
    */
    private static class ClassPathContextResource extends ClassPathResource implements ContextResource {

        public ClassPathContextResource(String path, ClassLoader classLoader) {
            super(path, classLoader);
        }

        public String getPathWithinContext() {
            return getPath();
        }

        @Override
        public Resource createRelative(String relativePath) {
            String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
            return new ClassPathContextResource(pathToUse, getClassLoader());
        }
    }
           

XmlWebApplicationContext的父类AbstractRefreshableWebApplicationContext重写了getResourceByPath(String path)方法,代码如下。

@Override
    protected Resource getResourceByPath(String path) {
        return new ServletContextResource(this.servletContext, path);
    }
           

ServletContextResource代表的文件是相对于web容器根目录的,通过它的下面一段代码就一目了然了。

public InputStream getInputStream() throws IOException {
        InputStream is = this.servletContext.getResourceAsStream(this.path);
        if (is == null) {
            throw new FileNotFoundException("Could not open " + getDescription());
        }
        return is;
    }
           

因此在web应用中,spring会把无前缀的location当成是web容器根目录下的某个文件。

2 根据模式匹配加载多个资源

所谓的模式匹配也就是location参数使用了通配符,比如’*’、’?’等,在spring中,location参数为下面3中情况时,会加载多个资源

1. 使用ant风格的通配符

2. 以classpath*:为前缀

3. 以上两种同用

Spring通过实现ResoureLoader的子接口ResourcePatternResolver来加载多个资源文件。其中,XmlWebApplicationContext实现了ResourcePatternResolver接口,而此接口的getResources(String locationPattern)方法已在XmlWebApplicationContext的父类AbstractApplicationContext中实现了,代码如下。

public Resource[] getResources(String locationPattern) throws IOException {
        // 把获取资源的实现委托给其他ResourcePatternResolver,默认为PathMatchingResourcePatternResolver
        return this.resourcePatternResolver.getResources(locationPattern);
    }
           

这段代码把加载指定模式的资源的任务委托给PathMatchingResourcePatternResolver的getResources(String locationPattern)方法,这个方法的代码如下,

public Resource[] getResources(String locationPattern) throws IOException {
        Assert.notNull(locationPattern, "Location pattern must not be null");
        // 在ResourcePatternResolver接口中声明:String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
        if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
            // 处理classpath*:前缀的location配置
            // 这里默认的模式匹配器是AntPathMatcher,即处理ant风格的匹配
            if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
                // 查找与匹配模式匹配的资源,详见2.2.3
                return findPathMatchingResources(locationPattern);
            } else {
                // 没有使用通配符,返回classes路径下和所有jar包中的所有相匹配的资源
                return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
            }
        } else {
            // Note:这里只会查找第一个根目录下面的所有相匹配的资源

            // 检查模式是否被匹配器匹配
            int prefixEnd = locationPattern.indexOf(":") + ;
            if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
                // 查找与匹配模式匹配的资源
                return findPathMatchingResources(locationPattern);
            }
            else {
                // locationPattern没有使用通配符
                // 只加载第一个找到的资源,默认使用DefaultResourceLoader的getResource方法
                // 详见1部分
                return new Resource[] {getResourceLoader().getResource(locationPattern)};
            }
        }
    }
           

这段代码主要是对locationPattern参数做分类,然后根据不同的分类调用相应的处理方法,它把locationPattern分成以下四种情况:

第一种是前缀为classpath*:且含有通配符,这种情况将查找与匹配模式匹配的所有资源;

第二种是前缀为classpath*:但不含通配符,这种情况返回classes路径和jar包中匹配的所有资源;

第三种是前缀不为classpath*:为前缀且含有通配符,这种情况与第一种情况有点类似,同样是查找与匹配器相匹配的资源,但只返回找到的第一个根目录下的所有与匹配模式匹配的资源;

第四种是前缀不为classpath*:为前缀且不含通配符,这种情况只返回查找到的第一个资源,详见第1节——根据具体的路径加载资源。

上面代码spring的开发者写的有点绕,仔细分析这段代码,对于第一种情况和第三种情况,判断逻辑其实都是一样的,开发者完全可以把这段代码合成一段。下面是我通过继承PathMatchingResourcePatternResolver重写了getResources接口,代码如下。点此下载

public class MyPathMatchingResourcePatternResolver extends PathMatchingResourcePatternResolver
{

    public MyPathMatchingResourcePatternResolver(ClassLoader classLoader) {
        super(classLoader);
    }

    @Override
    public Resource[] getResources(String locationPattern) throws IOException {

        int prefixEnd = locationPattern.indexOf(":") + ;
        if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
            // 查找与匹配模式匹配的资源
            return findPathMatchingResources(locationPattern);
        }

        // 不使用通配符
        if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
            // 返回classes路径下和所有jar包中的所有相匹配的资源
            return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
        }

        // 只加载一个资源
        return new Resource[] {getResourceLoader().getResource(locationPattern)};
    }
}
           

这段代码我只把locationPattern分成三种情况:第一种是含有通配符的,第二种是以classpath*:为前缀但没有使用通配符的,第三种是没有以classpath*:为前缀也没有使用通配符的。

对于只加载一个资源的情况已经在第1节中探讨了。下面先来看看spring如何处理以classpath*:为前缀但没使用通配符情况,再回过头来看spring如何处理使用通配符的情况。

2.1 加载与以classpath*:为前缀但没有使用通配符的location相匹配的资源。

在PathMatchingResourcePatternResolver的getResources(String locationPattern)方法中对于以classpath*:为前缀但没有使用通配符的location参数是通过调用它的findAllClassPathResources(String location)方法来创建相应的Resource对象的,代码如下。

protected Resource[] findAllClassPathResources(String location) throws IOException {
        String path = location;
        if (path.startsWith("/")) {
            path = path.substring();
        }
        Set<Resource> result = doFindAllClassPathResources(path);
        if (logger.isDebugEnabled()) {
            logger.debug("Resolved classpath location [" + location + "] to resources " + result);
        }
        return result.toArray(new Resource[result.size()]);
    }
           
protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
        Set<Resource> result = new LinkedHashSet<Resource>();
        ClassLoader cl = getClassLoader();
        // 获取class路径和jar包下相匹配的文件url
        Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
        while (resourceUrls.hasMoreElements()) {
            URL url = resourceUrls.nextElement();
            result.add(convertClassLoaderURL(url));
        }
        if ("".equals(path)) {
            // 获取所有的jar包
            addAllClassLoaderJarRoots(cl, result);
        }
        return result;
    }
           
/**
     * 返回一个UrlResource对象
     */
    protected Resource convertClassLoaderURL(URL url) {
        return new UrlResource(url);
    }
           

findAllClassPathResources方法通过ClassLoader对象来加载指定名称的资源,不管它在classes路径下还是任何jar包中。如果path参数为空字符串,那么将调用addAllClassLoaderJarRoots方法获取所有jar包。

2.2 加载与含有通配符的location参数相匹配的资源。

如果location参数中使用了通配符,那么PathMatchingResourcePatternResolver将调用它的findPathMatchingResources(String locationPattern)方法,代码如下。

protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {

        // 获取文件所在的根目录,不含通配符
        String rootDirPath = determineRootDir(locationPattern);
        // 获取含有通配符部分的字符串
        String subPattern = locationPattern.substring(rootDirPath.length());
        / 把根目录封装成Resource对象
        Resource[] rootDirResources = getResources(rootDirPath);
         // 遍历查找到的根目录资源,这些根目录可能是来自class路径、jar包、zip等其他压缩包等
        Set<Resource> result = new LinkedHashSet<Resource>();
        for (Resource rootDirResource : rootDirResources) {
            rootDirResource = resolveRootDirResource(rootDirResource);
            URL rootDirURL = rootDirResource.getURL();
            if (equinoxResolveMethod != null) {
                if (rootDirURL.getProtocol().startsWith("bundle")) {
                    // 执行resolver方法进行协议转换
                    rootDirURL = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirURL);
                    rootDirResource = new UrlResource(rootDirURL);
                }
            }
            if (rootDirURL.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
                // 从vfs中加载匹配的资源
                result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirURL, subPattern, getPathMatcher()));
            }
            else if (ResourceUtils.isJarURL(rootDirURL) || isJarResource(rootDirResource)) {
                // 从jar包中加载匹配的资源
                result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirURL, subPattern));
            }
            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()]);
    }
           
/**
    * 根据指定location查找根目录。比如location为“/WEB-INF/config/*.xml”,根目录为“/WEB-INF/config/”
    */
    protected String determineRootDir(String location) {
        int prefixEnd = location.indexOf(":") + ;
        int rootDirEnd = location.length();
        while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) {
            rootDirEnd = location.lastIndexOf('/', rootDirEnd - ) + ;
        }
        if (rootDirEnd == ) {
            rootDirEnd = prefixEnd;
        }
        return location.substring(, rootDirEnd);
    }
           

findPathMatchingResources方法主要是获取根目录资源,然后根据根目录的类型调用相应的方法来获取根目录下的资源。它把根目录分为三类,其一vfs中的目录,其二是jar包中的目录,其三是文件系统中的目录。

(1)加载vfs下的资源

如果目录在vfs中,PathMatchingResourcePatternResolver会调用它的私有静态内部类VfsResourceMatchingDelegate 的静态方法findMatchingResources来加载vfs中的资源,代码如下。

private static class VfsResourceMatchingDelegate {

        public static Set<Resource> findMatchingResources(
                URL rootDirURL, String locationPattern, PathMatcher pathMatcher) throws IOException {

            Object root = VfsPatternUtils.findRoot(rootDirURL);
            PatternVirtualFileVisitor visitor =
                    new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
            VfsPatternUtils.visit(root, visitor);
            return visitor.getResources();
        }
    }
           

(2)加载jar包目录下的资源

如果目录在jar包中,PathMatchingResourcePatternResolver执行 doFindPathMatchingJarResources(Resource rootDirResource, URL rootDirURL, String subPattern)方法,代码如下。

protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, URL rootDirURL, String subPattern)
            throws IOException {

        Set<Resource> result = doFindPathMatchingJarResources(rootDirResource, subPattern);
        if (result != null) {
            return result;
        }

        URLConnection con = rootDirURL.openConnection();
        JarFile jarFile;
        String jarFileUrl;
        String rootEntryPath;
        boolean closeJarFile;

        if (con instanceof JarURLConnection) {
            // Should usually be the case for traditional JAR files.
            JarURLConnection jarCon = (JarURLConnection) con;
            ResourceUtils.useCachesIfNecessary(jarCon);
            jarFile = jarCon.getJarFile();
            jarFileUrl = jarCon.getJarFileURL().toExternalForm();
            JarEntry jarEntry = jarCon.getJarEntry();
            rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
            closeJarFile = !jarCon.getUseCaches();
        }
        else {
            String urlFile = rootDirURL.getFile();
            try {
                // 声明:public static final String JAR_URL_SEPARATOR = "!/";
                int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
                if (separatorIndex != -) {
                    jarFileUrl = urlFile.substring(, separatorIndex);
                    rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
                    jarFile = getJarFile(jarFileUrl);
                } else {
                    jarFile = new JarFile(urlFile);
                    jarFileUrl = urlFile;
                    rootEntryPath = "";
                }
                closeJarFile = true;
            } catch (ZipException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]");
                }
                return Collections.emptySet();
            }
        }

        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
            }
            if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
                // 确保rootEntryPath以'/'结尾
                rootEntryPath = rootEntryPath + "/";
            }
            result = new LinkedHashSet<Resource>();
            // 遍历目录下的文件
            for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
                JarEntry entry = entries.nextElement();
                String entryPath = entry.getName();
                if (entryPath.startsWith(rootEntryPath)) {
                    String relativePath = entryPath.substring(rootEntryPath.length());
                    // 判断当前资源路径是否与指定模式匹配
                    if (getPathMatcher().match(subPattern, relativePath)) {
                        result.add(rootDirResource.createRelative(relativePath));
                    }
                }
            }
            return result;
        } finally {
            if (closeJarFile) {
                jarFile.close();
            }
        }
    }
           
protected JarFile getJarFile(String jarFileUrl) throws IOException {
        // 声明:public static final String FILE_URL_PREFIX = "file:";
        if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
            try {
                return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart());
            } catch (URISyntaxException ex) {
                return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length()));
            }
        }
        else {
            return new JarFile(jarFileUrl);
        }
    }
           

(3)从文件系统中加载资源

如果根目录不在jar包或者vfs中,PathMatchingResourcePatternResolver会把根目录当成本地文件系统中的目录,调用它的 doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)方法来实现,这个方法的代码如下。

protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
            throws IOException {

        File rootDir;
        try {
            // 获取根目录File对象
            rootDir = rootDirResource.getFile().getAbsoluteFile();
        }
        catch (IOException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Cannot search for matching files underneath " + rootDirResource +
                        " because it does not correspond to a directory in the file system", ex);
            }
            return Collections.emptySet();
        }

        // 从文件系统中查找匹配的资源
        return doFindMatchingFileSystemResources(rootDir, subPattern);
    }
           

上面代码主要是获取根目录文件,同时也检查根目录是否存在,然后调用PathMatchingResourcePatternResolver对象的doFindMatchingFileSystemResources方法,代码如下。

/**
    * 从文件系统中查找匹配的资源
    */
    protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException {
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for matching resources in directory tree [" + rootDir.getPath() + "]");
        }
        // 获取匹配的文件
        Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern);
        Set<Resource> result = new LinkedHashSet<Resource>(matchingFiles.size());
        for (File file : matchingFiles) {
            // 使用FileSystemResource对象封装匹配的文件对象
            result.add(new FileSystemResource(file));
        }
        return result;
    }
           

doFindMatchingFileSystemResources方法主要做的事情是调用PathMatchingResourcePatternResolver对象的retrieveMatchingFiles方法来获取根目录下与指定模式匹配的文件(见下面代码)。然后把匹配的文件封装到FileSystemResource对象中。

/**
    * 获取匹配的文件
    */
    protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException {
        // 判断根文件是否存在
        if (!rootDir.exists()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping [" + rootDir.getAbsolutePath() + "] because it does not exist");
            }
            return Collections.emptySet();
        }
        // 判断根文件是否是目录文件
        if (!rootDir.isDirectory()) {
            // Complain louder if it exists but is no directory.
            if (logger.isWarnEnabled()) {
                logger.warn("Skipping [" + rootDir.getAbsolutePath() + "] because it does not denote a directory");
            }
            return Collections.emptySet();
        }
        // 判断根目录是否可读
        if (!rootDir.canRead()) {
            if (logger.isWarnEnabled()) {
                logger.warn("Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() +
                        "] because the application is not allowed to read the directory");
            }
            return Collections.emptySet();
        }
        // 获取完整的模式路径
        String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/");
        if (!pattern.startsWith("/")) {
            fullPattern += "/";
        }
        fullPattern = fullPattern + StringUtils.replace(pattern, File.separator, "/");
        Set<File> result = new LinkedHashSet<File>();
        // 把匹配的文件放到result对象中
        doRetrieveMatchingFiles(fullPattern, rootDir, result);
        return result;
    }
           

retrieveMatchingFiles方法主要做的事情是保证传入的根文件必须存在、必须目录和必须可读,以及调用doRetrieveMatchingFiles方法来获取匹配的文件。

protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException {
        if (logger.isDebugEnabled()) {
            logger.debug("Searching directory [" + dir.getAbsolutePath() +
                    "] for files matching pattern [" + fullPattern + "]");
        }
        // 获取目录中所有的文件
        File[] dirContents = dir.listFiles();
        if (dirContents == null) {
            if (logger.isWarnEnabled()) {
                logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
            }
            return;
        }
        Arrays.sort(dirContents);
        // 遍历目录中的文件
        for (File content : dirContents) {
            String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
            // 检查子文件是否为目录,且是否与指定的模式开头部分匹配
            if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
                if (!content.canRead()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() +
                                "] because the application is not allowed to read the directory");
                    }
                } else {
                    // 递归扫描子目录
                    doRetrieveMatchingFiles(fullPattern, content, result);
                }
            }
            // 检查子文件路径是否与指定的模全匹配
            if (getPathMatcher().match(fullPattern, currPath)) {
                result.add(content);
            }
        }
    }
           

doRetrieveMatchingFiles方法是获取匹配文件的终极方法,这个方法遍历指定的根目录下的所有文件,并把匹配的文件放到指定的Set对象中。

总结

spring支持的location前缀有多种,可以为任何网络协议为,比如http:、ftp:、file:等。也可以为classpath:、classpath*:,此时加载的为classes路径下和jar包中的文件。也可以没有前缀,此时加载的是相对于当前资源所在路径下的文件。

关于classpath:和classpath*:的区别。classpath:扫描的范围更小,它只加载第一个匹配的根目录下所匹配的资源;classpath*:扫描的范围更广,它查找所有匹配的根目录下所匹配的资源。

spring支持ant风格的location。但是要加载的资源为其他服务器上的资源,不能使用ant风格,必须为一个明确的地址,比如http://special.csdncms.csdn.net/programmer-covers。