spring Spring中application.properties中指定相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36940458/
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
Specifying relative path in application.properties in Spring
提问by Somasundaram Sekar
Is there a way we can lookup file resources using relative path in application.properties file in Spring boot application as specified below
有没有一种方法可以使用如下指定的 Spring boot 应用程序中 application.properties 文件中的相对路径查找文件资源
spring.datasource.url=jdbc:hsqldb:file:${project.basedir}/db/init
采纳答案by Abhijit Sarkar
@membersound answer is just breaking up the hardcoded path in 2 parts, not dynamically resolving the property. I can tell you how to achieve what you're looking for, but you need to understand is that there is NOproject.basedir
when you're running the application as a jar or war. Outside the local workspace, the source code structure doesn't exist.
@membersound 答案只是将硬编码路径分为两部分,而不是动态解析属性。我可以告诉你如何实现你要找的东西,但你要明白的是,有NOproject.basedir
,当你运行的应用程序作为一个罐子或War。在本地工作区之外,源代码结构不存在。
If you still want to do this for testing, that's feasible and what you need is to manipulate the PropertySource
s. Your simplest option is as follows:
如果您仍然想这样做以进行测试,那是可行的,您需要的是操作PropertySource
s。您最简单的选择如下:
Define an ApplicationContextInitializer
, and set the property there. Something like the following:
定义一个ApplicationContextInitializer
, 并在那里设置属性。类似于以下内容:
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext appCtx) {
try {
// should be /<path-to-projectBasedir>/build/classes/main/
File pwd = new File(getClass().getResource("/").toURI());
String projectDir = pwd.getParentFile().getParentFile().getParent();
String conf = new File(projectDir, "db/init").getAbsolutePath();
Map<String, Object> props = new HashMap<>();
props.put("spring.datasource.url", conf);
MapPropertySource mapPropertySource = new MapPropertySource("db-props", props);
appCtx.getEnvironment().getPropertySources().addFirst(mapPropertySource);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}}
Looks like you're using Boot, so you can just declare context.initializer.classes=com.example.MyApplicationContextInitializer
in your application.properties
and Boot will run this class at startup.
看起来你正在使用 Boot,所以你可以context.initializer.classes=com.example.MyApplicationContextInitializer
在你的中声明application.properties
,Boot 将在启动时运行这个类。
Words of cautionagain:
再次警告:
This will not work outside the local workspace as it depends on the source code structure.
I've assumed a Gradle project structure here
/build/classes/main
. If necessary, adjust according to your build tool.If
MyApplicationContextInitializer
is in thesrc/test/java
,pwd
will be<projectBasedir>/build/classes/test/
, not<projectBasedir>/build/classes/main/
.
这在本地工作区之外不起作用,因为它取决于源代码结构。
我在这里假设了一个 Gradle 项目结构
/build/classes/main
。如有必要,请根据您的构建工具进行调整。如果
MyApplicationContextInitializer
是在src/test/java
,pwd
将会是<projectBasedir>/build/classes/test/
,不是<projectBasedir>/build/classes/main/
。
回答by chendu
I'm using spring boot to build a upload sample, and meet the same problem, I only want to get the project root path. (e.g. /sring-boot-upload)
我在用spring boot构建一个上传示例,遇到同样的问题,我只想获取项目根路径。(例如 /sring-boot-upload)
I find out that below code works:
我发现下面的代码有效:
upload.dir.location=${user.dir}\uploadFolder
回答by membersound
your.basedir=${project.basedir}/db/init
spring.datasource.url=jdbc:hsqldb:file:${your.basedir}
@Value("${your.basedir}")
private String file;
new ClassPathResource(file).getURI().toString()