使用classpath:在春天

a L*_*ner 25 spring classpath

关于classpath:春季期权,我有两个问题: -

1)是否classpath:搜索相对于指定文档的资源(如果是Web应用程序)?

假设我使用以下内容:

<bean class="mybean">
<property name="myresource" value="classpath:myfile.txt"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

/WEB-INF/classes/config/myconfig.xml下的myconfig.xml中.然后从哪里开始搜索?

2)如果直接定位资源而不是给出classpath:ie ,搜索会更快吗?

<bean class="mybean">
<property name="myresource" value="classpath:/WEB-INF/classes/myfolder/myfile.txt"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

代替

<bean class="mybean">
<property name="myresource" value="classpath:myfile.txt"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

谢谢...

ska*_*man 17

是否classpath:搜索相对于指定文档的资源(如果是Web应用程序)?

不,classpath:总是相对于类路径根.如果你/在路径的开头放置一个,它将被静默删除.

如果我直接定位资源,例如,搜索更快 classpath:/WEB-INF/classes/myfolder/myfile.txt

不,那根本不起作用.类路径根包含/WEB-INF/classes,因此路径应该相对于该路径.

不要将classpath:路径与文件路径混淆,它们彼此没有关系.


小智 5

看看http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards

这个特殊的前缀指定必须获取与给定名称匹配的所有类路径资源(在内部,这基本上是通过 ClassLoader.getResources(...) 调用发生的),然后合并以形成最终的应用程序上下文定义。

所以classpath:从你的类路径的根开始。