简体   繁体   English

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

[英]What is the difference between “classpath:” and “classpath:/” in Spring XML?

I am working on some Spring XML configuration files and sometimes they use "classpath:/datasource.xml" and sometimes "classpath:datasource.xml" . 我正在处理一些Spring XML配置文件,有时它们使用"classpath:/datasource.xml" ,有时使用"classpath:datasource.xml" Is there a difference between the two or is the leading / optional / implied / redundant? 这两者之间是否存在差异,或者是前导/可选/隐含/冗余?

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". “classpath *:”前缀也可以与位置路径的其余部分中的PathMatcher模式组合,例如“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. 在这种情况下,解析策略非常简单:在最后一个非通配符路径段上使用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. 这意味着像“classpath *:*。xml”这样的模式不会从jar文件的根目录中检索文件,而只能从扩展目录的根目录中检索文件。 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). 这源于JDK的ClassLoader.getResources()方法中的限制,该方法仅返回传入的空字符串的文件系统位置(指示搜索的潜在根)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM