天天看點

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。