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

丶灬走出姿态 提交于 2020-01-10 13:04:36

问题


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?


回答1:


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

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.

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).



来源:https://stackoverflow.com/questions/13994840/what-is-the-difference-between-classpath-and-classpath-in-spring-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!