Spring的PathMatchingResourcePatternResolver使用classpath*时jar包资源无法匹配BUG

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里的代码:

  1. protected Resource[] findAllClassPathResources(String location) throws IOException {
  2. String path = location;
  3. if (path.startsWith("/")) {
  4. path = path.substring(1);
  5. }
  6. 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

搞掂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值