我正在尝试从 jar 文件外部导入 spring config.xml。有一个注释配置类可以将其导入到配置中
@Configuration
@ImportResource("file:config.xml")
public class SpringConfig {
...
}
来自 spring 的日志消息表明它找不到 config.xml。但他们没有说明搜索此 config.xml 的路径是什么。
Caused by: java.io.FileNotFoundException: config.xml (The system cannot find the file specified)
在开发环境中
src\main\java\
|__com.packagewithclasses
|__config.xml
|__log4j.xml
在现场环境
folder_holding_jars_and_configs
|__executable_jar_file.jar
|__config.xml
|__log4j.xml
我尝试过文档、谷歌、一些通配符、日志等。没有运气。 spring 在 jar 文件外部配置的相对路径的起始路径是什么?
最佳答案
您必须提供文件的绝对路径,以防文件位于 jar 之外。
例如
@Configuration
@ImportResource("file:/opt/proj/config.xml")
public class SpringConfig {
...
}
编辑:
除上述之外,您还可以将 XML 文件的相对路径放入 MANIFEST.MF
文件中。由于您的 jar 和 XML 位于同一目录中,您可以将以下行放入 MANIFEST.MF
Class-Path: .
MANIFEST.MF
需要在文件末尾有一个空行。
现在您可以使用如下所示的 xml 文件。
@Configuration
@ImportResource("file:/config.xml")
public class SpringConfig {
...
}
对于 maven 项目,MENIFEST.MF
的位置为“src/main/resources/META-INF/MANIFEST.MF”。
关于java - spring中相对路径文件导入的起始路径是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41572856/