Java spring 类路径资源中的相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1601106/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:36:54  来源:igfitidea点击:

Relative paths in spring classpath resource

javaspringurlpathrelative-path

提问by Mike Q

I have a bunch of spring config files, all of which live under the META-INF directory in various subpackages. I have been using import like the following...

我有一堆 spring 配置文件,所有这些文件都位于各种子包中的 META-INF 目录下。我一直在使用如下导入...

  <import resource="../database/schema.xml"/>

So a relative path from the source file. This works fine when I am working with a local build outside of a jar file. But when I package everything up in a jar then I get an error that it cannot resolve the URL resource. If I change the above to an absolute path (with classpath:) then it works fine.

所以是源文件的相对路径。当我在 jar 文件之外使用本地构建时,这很好用。但是,当我将所有内容打包到 jar 中时,我收到一个错误,提示它无法解析 URL 资源。如果我将上面的内容更改为绝对路径(使用类路径:),那么它工作正常。

Is there any way to use relative paths with ".." in when the configs are packaged in a jar or am I restricted to descending relative paths and absolute paths only?

当配置打包在 jar 中时,有没有办法使用带有“..”的相对路径,或者我仅限于降序相对路径和绝对路径?

回答by Trick

<import resource="classpath:database/schema.xml"/>

回答by Daniel Kleine-Albers

A short addition: If you want to access the resources from a jar, it should read:

一个简短的补充:如果你想从一个 jar 访问资源,它应该是:

<import resource="classpath*:database/schema.xml"/>

回答by Pierre-Olivier Pignon

What works fine on my project is the following lines in the app-servlet.xml :

在我的项目中运行良好的是 app-servlet.xml 中的以下几行:

<!-- Database Configuration -->
<import resource="classpath*:/database/DataSource.xml"/>
<import resource="classpath*:/database/Hibernate.xml"/>

Directories

Directories

If it can help you ...

如果能帮到你...