使用类路径:在春季

发布于 2025-01-01 22:17:23 字数 753 浏览 2 评论 0原文

我有两个关于 Spring 中的 classpath: 选项的问题:-

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

假设我使用以下内容:

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

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

而不是

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

谢谢...,搜索是否会更快?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别闹i 2025-01-08 22:17:23

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

不,classpath: 始终相对于类路径根。如果您将 / 放在路径的开头,它将被静默删除。

如果我直接给出资源位置,搜索是否会更快,例如classpath:/WEB-INF/classes/myfolder/myfile.txt

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

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

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.

陌上芳菲 2025-01-08 22:17:23

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文