java 忽略“当前目录”中的 Spring Boot 外部属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40894087/
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
Spring boot external properties file in "current directory" is ignored
提问by clickcell
24.3?Application property files SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
24.3?Application 属性文件 SpringApplication 将从以下位置的 application.properties 文件加载属性并将它们添加到 Spring Environment:
当前目录的 /config 子目录。
当前目录
一个类路径 /config 包
类路径根
It mentions current directory twice but this really doesn't mean anything:
它两次提到当前目录,但这真的没有任何意义:
I tried putting it in the root of my project (i.e. above src
in the folder that matches the output of java.io.File( "." ).getCanonicalPath()
and System.getProperty("user.dir");
), and I tried putting it with the war files (i.e. in build\libs
)
我试图把它在我的项目的根(即上面src
说的输出相匹配的文件夹中java.io.File( "." ).getCanonicalPath()
和System.getProperty("user.dir");
),我试图把它与War的文件(即build\libs
)
But the only place to put it that actually works is the default location (src\main\resources
).
但唯一真正有效的放置位置是默认位置 ( src\main\resources
)。
So what does "current directory" even mean and where do the files really go?
那么“当前目录”到底是什么意思,文件到底去哪儿了?
I need to find the correct external location for the files so I don't have to build database credentials into the app.
我需要为文件找到正确的外部位置,这样我就不必将数据库凭据构建到应用程序中。
The guides say that putting application.properties
in current directory will work and I found the exact current directory to put it in but it still doesn't work, which I can verify by the output of: System.out.println(System.getProperty("spring.datasource.url"));
which is null
It does output the correct value only with an embedded properties file.
指南说,放入application.properties
当前目录会起作用,我找到了将它放入的确切当前目录,但它仍然不起作用,我可以通过以下输出进行验证:System.out.println(System.getProperty("spring.datasource.url"));
which is null
It do only output the right value with an Embedded属性文件。
回答by tan9
According to ConfigFileApplicationListener:
根据ConfigFileApplicationListener:
// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS =
"classpath:/,classpath:/config/,file:./,file:./config/";
file:./
resolve to the working directory where you start the java process.
file:./
解析到您启动 java 进程的工作目录。
回答by Sabir Khan
I agree with Stephane Nicoll's argument that we generally don't need this for development and test but needed for production where properties file is generally externalized and the one present in source code is not used. This is what works for me ,
我同意 Stephane Nicoll 的论点,即我们通常不需要在开发和测试中使用它,但在生产中需要它,其中属性文件通常被外部化并且不使用源代码中的文件。这对我有用,
java -jar myjar.jar --spring.config.location=file:D:\\RunRC\\application.properties
java -jar myjar.jar --spring.config.location=file:D:\\RunRC\\application.properties
Directory - D:\\RunRC
- mentioned in above command is sample from my machine.
目录 - D:\\RunRC
- 上面命令中提到的示例来自我的机器。
I keep using properties file of source code i.e. from \src\main\resources\
in development and test but in production , I comment out entries and if I am starting my jar or war from D:\\RunRC
then I provide Current Directoryas shown in above java command and keep properties file there.
我一直使用源代码的属性文件,即来自\src\main\resources\
开发和测试但在生产中,我注释掉条目,如果我D:\\RunRC
从那时开始我的 jar 或 war,我提供当前目录,如上面的 java 命令所示,并将属性文件保留在那里。
Just doing - @PropertySource({ "application.properties"})
or @PropertySource({ "file:application.properties"})
doesn't pick it up from the directory where jar or war is kept.
只是做 -@PropertySource({ "application.properties"})
或者@PropertySource({ "file:application.properties"})
不从保存 jar 或 war 的目录中提取它。
For database credentials, I would suggest to use OS specific environment variables and use syntax similar to - @PropertySource({"file:${CONF_DIR}database.properties" })
where CONF_DIR
is existing environment variable pointing to that directory.
对于数据库凭据,我建议使用操作系统特定的环境变量并使用类似于 - @PropertySource({"file:${CONF_DIR}database.properties" })
where CONF_DIR
is existing environment variable 指向该目录的语法。
Hope it helps !!
希望能帮助到你 !!
回答by Joe
回答by kimy82
I understand that the current directory is the root directory of your project. However, you can change this with -Dspring.config.location=your/config/dir/
.
Have a look at this post enter link description here
我了解到当前目录是您项目的根目录。但是,您可以使用-Dspring.config.location=your/config/dir/
. 看看这篇文章在这里输入链接描述
回答by Stephane Nicoll
If you search for "current directory java", you'll end up here with this question. The intention is that if you put an application.properties
in the same directory as the application, it will be picked up by default.
如果您搜索“当前目录 java”,您最终会遇到这个问题。目的是如果你把一个application.properties
和应用程序放在同一个目录下,它会被默认选中。
You will not use that feature in development or for test as you shouldn't rely on that feature. But when running your app in production, it might be handy to put environment-specific settings in a configuration file that sits next to the application itself.
您不会在开发或测试中使用该功能,因为您不应该依赖该功能。但是在生产中运行您的应用程序时,将特定于环境的设置放在应用程序本身旁边的配置文件中可能会很方便。