pathmatchingresourcepatternsolver-查找org.springframework.core.io.resource设置为“file:/”的原因

zc0qhyus  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(218)

我下面有一些代码,是用来扫描我的巴蒂斯Map文件。我正在扫描我的batis文件,但我认为我的问题更多的是spring问题。
列出的代码运行良好,但当我向存储在本地maven repo中的某个外部库添加依赖项时,pathmatchingresourcepatternresolver.getresources()方法会为特定jar返回“file:/”的资源。这导致spring在我的整个c驱动器中搜索mapper文件。
我能够确定是什么jar导致了这种行为,但我不确定解决方案是什么。为什么spring将资源设置为“file:/”?我不确定这个jar和其他返回正常资源条目的jar有什么问题。

SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource configLocation = patternResolver.getResource("classpath:mybatis/mybatis.config.xml");

 //this line leads to the issue
Resource[] mapperLocations1 = patternResolver.getResources("classpath*:**/*Mapper.xml");

//this is from the Spring PathMatchingResourcePatternResolver class
    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);
            URL rootDirUrl = rootDirResource.getURL();
            if (equinoxResolveMethod != null) {
                if (rootDirUrl.getProtocol().startsWith("bundle")) {
                    rootDirUrl = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirUrl);
                    rootDirResource = new UrlResource(rootDirUrl);
                }
            }
            if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
                result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher()));
            }
            else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) {
                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()]);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题