WEB开发网
开发学院软件开发Java Spring源码学习-含有通配符路径解析(上) 阅读

Spring源码学习-含有通配符路径解析(上)

 2012-05-24 09:29:39 来源:WEB开发网   
核心提示:如果路径包含通配符(?,*,**)spring是怎么处理的?如果是以classpath*开头的又是如何呢?先测试分析包含通配符(?)的,Spring源码学习-含有通配符路径解析(上),/** * 测试包含通配符:*,?的路径 * <p>D:\\workspace-home\\spring-custom\

如果路径包含通配符(?,*,**)spring是怎么处理的?如果是以classpath*开头的又是如何呢?
先测试分析包含通配符(?)的。
/**
* 测试包含通配符:*,?的路径
* <p>D:\\workspace-home\\spring-custom\\src\\main\\resources\\spring\\ap?-context.xml</p>
* 通过读取配置文件失败的情况,因为此时Spring不支持\\路径的通配符解析
*
* @author lihzh
* @date 2012-5-5 上午10:53:53
*/
@Test
public void testAntStylePathFail() {
String pathOne = "D:\\workspace-home\\spring-custom\\src\\main\\resources\\spring\\ap?-context.xml";
ApplicationContext appContext = new FileSystemXmlApplicationContext(pathOne);
assertNotNull(appContext);
VeryCommonBean bean = null;
try {
bean = appContext.getBean(VeryCommonBean.class);
fail("Should not find the [VeryCommonBean].");
} catch (NoSuchBeanDefinitionException e) {
}
assertNull(bean);
}
正如测试用例所写,实际是找不到该Bean的。这又是为什么?Spring不是支持通配符吗?FileSystemXmlApplicationContext的注释里也提到了通配符的情况:
* <p>The config location defaults can be overridden via [email protected] #getConfigLocations},
* Config locations can either denote concrete files like "/myfiles/context.xml"
* or Ant-style patterns like "/myfiles/*-context.xml" (see the
* [email protected] org.springframework.util.AntPathMatcher} javadoc for pattern details).
从代码中寻找答案。回到上回的else分支中,因为包含通配符,所以进入第一个子分支。
/**
* Find all resources that match the given location pattern via the
* Ant-style PathMatcher. Supports resources in jar files and zip files
* and in the file system.
* @param locationPattern the location pattern to match
* @return the result as Resource array
* @throws IOException in case of I/O errors
* @see #doFindPathMatchingJarResources
* @see #doFindPathMatchingFileResources
* @see org.springframework.util.PathMatcher
*/
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));
}

1 2 3 4 5 6  下一页

Tags:Spring 源码 学习

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接