今天在查看Spring的PathMatchingResourcePatternResolver类代码时,有调用到ClassLoader.getResources()方法,
忽然发现一直没弄清 ClassLoader.getResources() 在什么时候会返回多于1个URL?
能否举个简单的例子 谢谢!
[b]问题补充:[/b]
刚才试了一下,发现如果类路径上有重复的资源,getResource()方法会返回类路径上碰到的第一个资源。
而getResources()则会返回当前类加载器路径上的所有重复资源以及父类加载器上的所有重复资源。
比如,在tomcat\lib目录下放置一个zip文件,包含config/aaa.txt文件,
在WEB-INF/lib目录下复制一份这个zip文件,
再在src目录下放上config/aaa.txt文件,
getResource("config/aaa.txt")返回结果是:
file:/D:/eclipse/workspace/demo/WebContent/WEB-INF/classes/config/aaa.txt
getResources("config/aaa.txt")返回结果是:
file:/D:/eclipse/workspace/demo/WebContent/WEB-INF/classes/config/aaa.txt
jar:file:/D:/apache-tomcat-6.0.16/lib/aaaaa.jar!/config/aaa.txt
jar:file:/D:/eclipse/workspace/demo/WebContent/WEB-INF/lib/bbbbb.jar!/config/aaa.txt