Spring版本:3.0.5
Spring提供了一个强大的resource loader,其中的PathMatchingResourcePatternResolver能够处理jar包里的资源。使用ant的正则用起来也应该挺舒服,但是一个看似简单的正则式,匹配jar包的资源却失败了:
classpath*:**/*CodeEnum.class
乍一看**/*CodeEnum.class这样的表达式在ant也是100%通过的,怎么匹配jar包的资源就不行了呢。还是开源强大,把source配上,一路断点跟了进去。
发现在这行语句开始出问题了:
return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
debug值:
locationPattern=classpath*:
CLASSPATH_ALL_URL_PREFIX=classpath*:
结果substring后,就变成执行findAllClassPathResources("")了。再看findAllClassPathResources里的代码:
- protected Resource[] findAllClassPathResources(String location) throws IOException {
- String path = location;
- if (path.startsWith("/")) {
- path = path.substring(1);
- }
- Enumeration resourceUrls = getClassLoader().getResources(path); //<--path是空串,难怪找不到jar里的资源了
protected Resource[] findAllClassPathResources(String location) throws IOException {
String path = location;
if (path.startsWith("/")) {
path = path.substring(1);
}
Enumeration resourceUrls = getClassLoader().getResources(path); //<--path是空串,难怪找不到jar里的资源了
所以解决办法就是至少将第一个包名添加上去就没问题了:
classpath*:com/**/*CodeEnum.class
搞掂