java Spring XML 中的“classpath:”和“classpath:/”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13994840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:40:56  来源:igfitidea点击:

What is the difference between "classpath:" and "classpath:/" in Spring XML?

javaspringclasspath

提问by Sled

I am working on some Spring XML configuration files and sometimes they use "classpath:/datasource.xml"and sometimes "classpath:datasource.xml". Is there a difference between the two or is the leading /optional / implied / redundant?

我工作的一些Spring XML配置文件,有时他们使用"classpath:/datasource.xml",有时"classpath:datasource.xml"。两者之间有区别还是领先的/可选/隐含/多余?

回答by muruga

I don't see any difference between these two. The biggest difference that you will see is that the relative path and the * on the classpath location

我看不出这两者有什么区别。您将看到的最大区别是相对路径和类路径位置上的 *

Here is an excerpt from Spring Resources, look for section 4.7.2.2

这是Spring Resources的摘录,查找第 4.7.2.2 节

Classpath*:
The "classpath*:" prefix can also be combined with a PathMatcher pattern in the rest of the location path, for example "classpath*:META-INF/*-beans.xml". In this case, the resolution strategy is fairly simple: a ClassLoader.getResources() call is used on the last non-wildcard path segment to get all the matching resources in the class loader hierarchy, and then off each resource the same PathMatcher resoltion strategy described above is used for the wildcard subpath.

Classpath*:
“classpath*:”前缀也可以与位置路径其余部分中的PathMatcher 模式组合,例如“classpath*:META-INF/*-beans.xml”。在这种情况下,解析策略相当简单:在最后一个非通配符路径段上使用 ClassLoader.getResources() 调用来获取类加载器层次结构中的所有匹配资源,然后对每个资源使用相同的 PathMatcher 解析策略上面描述的用于通配符子路径。

This means that a pattern like "classpath*:*.xml"will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK's ClassLoader.getResources() method which only returns file system locations for a passed-in empty string (indicating potential roots to search).

这意味着像“classpath*:*.xml”这样的模式不会从 jar 文件的根目录检索文件,而只会从扩展目录的根目录检索文件。这源于 JDK 的 ClassLoader.getResources() 方法中的限制,该方法仅返回传入空字符串的文件系统位置(指示要搜索的潜在根)。