繁体   English   中英

spring boot 外部配置

[英]spring boot external config

我正在尝试将外部属性文件加载到我的 Spring Boot 应用程序中。 最初我在配置类中使用了 @PropertySource。 但现在我想删除此注释,以便该类不依赖于位置。 所以我尝试使用:

java -jar my-boot-ws.war --SPRING_CONFIG_NAME=file:///Users/TMP/resources/

基于这个http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html文档,但我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder

使用注释工作正常,但我真的很想摆脱它。 对此的任何帮助都会很棒

谢谢

****** 更正 *******

抱歉,上面的命令应该是复制粘贴错误:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/

我不想更改配置文件的名称,只是添加了一个额外的位置。 正如这里所解释的:

如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且在加载之前会附加从 spring.config.name 生成的名称)。

我将此解释为文件 ${spring.application.name}.properties 将从命令行传入的 --spring.config.location 加载

经过更多的谷歌搜索,我发现这个Spring Boot 和多个外部配置文件表明以下是正确的用法:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties

我的印象是 --spring.config.location 会加载指定目录中的其他属性文件。 根据我提到的链接中的帖子,情况并非如此。 如果指定了目录,则基于链接,这就是搜索 application.properties 的位置。 但是这里的文档http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html似乎暗示spring boot 应用程序将在类路径上查看首先,如果可用,获取应用程序名称以获取基于该名称的其他属性文件。

然而,一旦我指定了一个文件名,一切都很好,所以我想我错了。

在命令行中,您应该使用以下属性来提及额外的引导配置文件:

--spring.config.location="file:/path/to/application.properties"

另一种选择是:

-Dspring.config.location="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

这可能不是一个常见问题,但我遇到了。 即使您用--spring.config.name替换它,您的类路径中也必须有一个application.properties (由于敏感信息,我在 gitignore 中有我的)。

1) 确保 args 在 run 方法内部传递

public class GemFireTestLoaderApplication {

public static void main(String[] args) {

        SpringApplication.run(GemFireTestLoaderApplication.class, args);
        }
}

2)如果你已经在xml注释中配置或者先删除

<!-- <context:property-placeholder location="classpath:config.properties" />  -->

<!--   <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> -->

您可以使用以下命令传递属性名称

3.1)

java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties

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

spring.config.name=spring  
spring.config.location=classpath:/config/

在侧面配置文件夹spring.properties文件可用,而在运行服务器时,此属性文件未加载

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM