Java spring boot 外部配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26870785/
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 config
提问by peekay
I am trying to load an external properties file into my spring boot app. initially I used @PropertySource in the config class. but now I want to remove this annotation so the class is not dependent on the location. so I tried to use:
我正在尝试将外部属性文件加载到我的 Spring Boot 应用程序中。最初我在配置类中使用了 @PropertySource。但现在我想删除此注释,以便该类不依赖于位置。所以我尝试使用:
java -jar my-boot-ws.war --SPRING_CONFIG_NAME=file:///Users/TMP/resources/
based on this http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.htmldocumentation but I get the following error:
基于这个http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html文档,但我收到以下错误:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder
using the annotation works fine but I would really like to move away from that. any help on this would be great
使用注释工作正常,但我真的很想摆脱它。对此的任何帮助都会很棒
Thanks
谢谢
****** CORRECTION *******
****** 更正 *******
Sorry copy paste error the above command was supposed to be:
抱歉,上面的命令应该是复制粘贴错误:
java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/
I'm not trying to change the name of the config file just add an additional location. As explained here:
我不想更改配置文件的名称,只是添加了一个额外的位置。正如这里所解释的:
If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded).
如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且在加载之前会附加从 spring.config.name 生成的名称)。
I interpreted this as saying that the file ${spring.application.name}.properties would be loaded from the --spring.config.location passed in from the command line
我将此解释为文件 ${spring.application.name}.properties 将从命令行传入的 --spring.config.location 加载
采纳答案by peekay
After some more googeling I found this Spring Boot and multiple external configuration filesindicating that the following is the correct usage:
经过更多的谷歌搜索,我发现这个Spring Boot 和多个外部配置文件表明以下是正确的用法:
java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties
I was under the impression that the --spring.config.location would load other properties files in the directory specified. according to the post at the link I mentioned this is not the case. based on the link if the directory is specified then that is where the application.properties is searched for. but again the documentation here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.htmlseems to insinuate that the spring boot app will look on the class path first and if available grab the app name to get additional properties files based on that name.
我的印象是 --spring.config.location 会加载指定目录中的其他属性文件。根据我提到的链接中的帖子,情况并非如此。如果指定了目录,则基于链接,这就是搜索 application.properties 的位置。但是这里的文档http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html似乎暗示spring boot 应用程序将在类路径上查看首先,如果可用,获取应用程序名称以获取基于该名称的其他属性文件。
however once I specified a file name everything worked fine so I guess I was mistaken.
然而,一旦我指定了一个文件名,一切都很好,所以我想我错了。
回答by tmarwen
In command line you should use below property to mention an additional boot configuration file:
在命令行中,您应该使用以下属性来提及额外的引导配置文件:
--spring.config.location="file:/path/to/application.properties"
An alternative would be:
另一种选择是:
-Dspring.config.location="file:/path/to/application.properties"
Note that characters are lower case and the word separator is a period ..
请注意,字符是小写的,单词分隔符是句点。.
Otherwise you can use an environment variable with key you used already:
否则,您可以使用带有您已经使用过的密钥的环境变量:
In a *nix system:
export SPRING_CONFIG_NAME=file:/path/to/application.properties
In Windows OS:
set SPRING_CONFIG_NAME=file:/path/to/application.properties
在 *nix 系统中:
export SPRING_CONFIG_NAME=file:/path/to/application.properties
在 Windows 操作系统中:
set SPRING_CONFIG_NAME=file:/path/to/application.properties
回答by lgonzales
It might not be a common issue, but I faced it. You also must have an application.properties
inside your classpath even when you replace it with --spring.config.name
(I had mine in gitignore due to sensitive information).
这可能不是一个常见的问题,但我遇到了。application.properties
即使您将其替换为--spring.config.name
(由于敏感信息,我在 gitignore 中有我的),您的类路径中 也必须有一个 。
回答by vaquar khan
1) Makesure args pass inside of run method
1) 确保 args 在 run 方法内部传递
public class GemFireTestLoaderApplication {
public static void main(String[] args) {
SpringApplication.run(GemFireTestLoaderApplication.class, args);
}
}
2) If you have configureed in xml comment or remove first
2)如果你已经在xml注释中配置或者先删除
<!-- <context:property-placeholder location="classpath:config.properties" /> -->
<!-- <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> -->
Following command you can use to pass properties name
您可以使用以下命令传递属性名称
3.1)
3.1)
java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
3.2)
3.2)
java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
回答by Suresh yadav
spring.config.name=spring
spring.config.location=classpath:/config/
in side the config folder spring.propertiesfile is available, while running the server the this properties file is not loading
在侧面配置文件夹spring.properties文件可用,而在运行服务器时,此属性文件未加载