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(":") + 1;
            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(":") + 1;
        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(1);
        }
        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>(16);
        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>(16);
        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(":") + 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);
    }

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 != -1) {
                    jarFileUrl = urlFile.substring(0, 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>(8);
            // 遍歷目錄下的文件
            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>(8);
        // 把匹配的文件放到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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章