Spring Boot外部配置 [英] spring boot external config

查看:103
本文介绍了Spring Boot外部配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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 文档,但出现以下错误:

based on this http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html documentation but I get the following error:

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

谢谢

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

****** 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:

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

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.application.name} .properties将从命令行传递的--spring.config.location加载

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

推荐答案

在进行了进一步的胶凝之后,我发现了这个

After some more googeling I found this Spring Boot and multiple external configuration files indicating that the following is the correct usage:

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应用程序将首先查看类路径,如果可用获取应用名称,以基于该名称获取其他属性文件.

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.html seems 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.

但是,一旦我指定了文件名,一切都可以正常工作,所以我想我记错了.

however once I specified a file name everything worked fine so I guess I was mistaken.

这篇关于Spring Boot外部配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆