using classpath: in spring

我只是一个虾纸丫 提交于 2019-12-17 22:24:30

问题


I have two questions regarding classpath: option in spring :-

1) Does classpath: search for resource relative to the document in which it is specified(in case of web applications)?

Suppose I use the following:

<bean class="mybean">
<property name="myresource" value="classpath:myfile.txt"/>
</bean>

in myconfig.xml under /WEB-INF/classes/config/myconfig.xml. Then from where it will start its search?

2)Is it faster to search if I give direct location of the resource instead of giving classpath: i.e

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


回答1:


Does classpath: search for resource relative to the document in which it is specified(in case of web applications)?

No, classpath: is always relative to the classpath root. If you put a / at the start of the path, it is silently removed.

Is it more fast to search if i give direct location of resource instead e.g. 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.

Don't confuse classpath: paths with file paths, they have no relation to each other.




回答2:


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.

So classpath: starts at the root of your classpath.



来源:https://stackoverflow.com/questions/9092713/using-classpath-in-spring

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