spring 使用类路径:在春天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9092713/
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
using classpath: in spring
提问by a Learner
I have two questions regarding classpath:option in spring :-
我有两个关于classpath:春季期权的问题:-
1) Does classpath:search for resource relative to the document in which it is specified(in case of web applications)?
1) 是否classpath:搜索与指定它的文档相关的资源(在 Web 应用程序的情况下)?
Suppose I use the following:
假设我使用以下内容:
<bean class="mybean">
<property name="myresource" value="classpath:myfile.txt"/>
</bean>
in myconfig.xmlunder /WEB-INF/classes/config/myconfig.xml. Then from where it will start its search?
在/WEB-INF/classes/config/myconfig.xml下的myconfig.xml 中。那么它将从哪里开始搜索呢?
2)Is it faster to search if I give direct location of the resource instead of giving classpath:i.e
2)如果我提供资源的直接位置而不是提供classpath:ie ,搜索速度是否更快
<bean class="mybean">
<property name="myresource" value="classpath:/WEB-INF/classes/myfolder/myfile.txt"/>
</bean>
instead of
代替
<bean class="mybean">
<property name="myresource" value="classpath:myfile.txt"/>
</bean>
Thanks...
谢谢...
回答by skaffman
Does
classpath:search for resource relative to the document in which it is specified(in case of web applications)?
是否
classpath:搜索与指定它的文档相关的资源(在 Web 应用程序的情况下)?
No, classpath:is always relative to the classpath root. If you put a /at the start of the path, it is silently removed.
不,classpath:总是相对于类路径根。如果将 a/放在路径的开头,它会被静默删除。
Is it more fast to search if i give direct location of resource instead e.g.
classpath:/WEB-INF/classes/myfolder/myfile.txt
如果我提供资源的直接位置而不是例如搜索是否更快
classpath:/WEB-INF/classes/myfolder/myfile.txt
No, that won't work at all. The classpath root contains /WEB-INF/classes, so the path should be relative to that.
不,那根本行不通。类路径根包含/WEB-INF/classes,所以路径应该是相对的。
Don't confuse classpath:paths with file paths, they have no relation to each other.
不要将classpath:路径与文件路径混淆,它们彼此没有关系。
回答by skaffman
Take a look at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards
This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...) call), and then merged to form the final application context definition.
这个特殊的前缀指定必须获取与给定名称匹配的所有类路径资源(在内部,这基本上是通过 ClassLoader.getResources(...) 调用发生的),然后合并以形成最终的应用程序上下文定义。
So classpath:starts at the root of your classpath.
所以classpath:从你的类路径的根开始。

