英文:
springboot: config in "additional-location" was overrided by "application-{profile}"
问题
以下是您要翻译的内容:
这是一个简单的Spring Boot项目,只有几个属性和一个MainClass
:
@Slf4j
@SpringBootApplication
public class DemoApplication {
@Value("${test}")
String test;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostConstruct
public void print() {
log.info(test);
}
}
它只是打印变量test
的值。
我在application.properties
中配置了test
:
test=test normal
spring.profiles.active=dev
变量被application-dev.properties
覆盖了:
test=test in dev
然后我运行了应用程序,它正常工作。它打印出:test in dev
接下来的问题是:
我将应用程序打包成了一个jar,并且当我运行这个应用程序时,我想覆盖test
,所以我编写了一个文件out.properties
:
test=test in out
通过以下命令启动应用程序:
java -jar target/demo.jar --spring.config.additional-location=out.properties
它仍然打印test in dev
!
我将命令更改为:
java -jar target/demo.jar --test="test in command"
它打印test in command
。
我已阅读文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config。
但我没有找到一些有用的提示。
我的目的是使用外部配置替换应用程序中的某些属性,这样我就不必更改源代码并重新打包。
感谢您的帮助!
英文:
It is a simple springboot project. only has a few properties and a MainClass
:
@Slf4j
@SpringBootApplication
public class DemoApplication {
@Value("${test}")
String test;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostConstruct
public void print() {
log.info(test);
}
}
It just print the value of variable test
.
I configure test
in application.properties
:
test=test normal
spring.profiles.active=dev
The variable was overrided by application-dev.properties
:
test=test in dev
Then I run the application, It works. It print: test in dev
The next thing is problem:
I package the application as a jar, and I want to override the test
when I run this application, So I write a file out.properties
:
test=test in out
Start the application by command
java -jar target/demo.jar --spring.config.additional-location=out.properties
It still prints test in dev
!
I change the command to:
java -jar target/demo.jar --test="test in command"
It print test in command
.
I have read the document: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config.
But I didn't find some useful tip.
My purpose is replace some property in application with a out config, so I don't have to change the source code and repackage.
Thanks for help!
答案1
得分: 2
链接的文档1明确列出了属性源的优先顺序:
1-3. ...
- 命令行参数。
5-11. ...
-
打包的 JAR 文件之外的特定配置文件(application-{profile}.properties 和 YAML 变体)。
-
打包的 JAR 文件内的特定配置文件(application-{profile}.properties 和 YAML 变体)。
-
打包的 JAR 文件之外的应用配置文件(application.properties 和 YAML 变体)。
-
打包的 JAR 文件内的应用配置文件(application.properties 和 YAML 变体)。
16-17. ...
正如你的代码已经显示的那样,(4) --test="test in command"
会覆盖 (12-15) 配置文件中的任何内容。
你还可以看到,(12-13) 特定配置文件 总是会覆盖 (14-15) 非特定配置文件。
因此,如果你希望外部文件覆盖 (13) 打包的 特定配置文件,那么你必须将该属性放在 (12) 外部 特定配置文件中。
但是,正如第 2.4 节 特定配置文件 中的说明:
> 如果在 spring.config.location
中指定了任何文件,则不会考虑文件的特定配置变体。如果您希 望还使用特定配置属性,请在 spring.config.location
中使用目录。
换句话说,spring.config.location
中列出的任何文件都是(14)应用配置文件,无论其命名方式如何,因此(12-13)的特定配置文件都会覆盖它们,无论是(13)打包的还是(12)外部的。
总结: 将 test=test in out
属性移动到位于 --spring.config.additional-location
路径中的一个名为 application-dev.properties
的目录中。
英文:
The linked documentation specifically lists the priority order of property sources:
1-3. ...
- Command line arguments.
5-11. ...
-
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
-
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
-
Application properties outside of your packaged jar (application.properties and YAML variants).
-
Application properties packaged inside your jar (application.properties and YAML variants).
16-17. ...
As your code already showed you, (4) --test="test in command"
overrides anything in (12-15) property files.
You can also see that (12-13) profile-specific application property files always override (14-15) non-profile application property files.
So, if you want an external file to override a (13) packaged profile-specific application property file, then you must place that property in a (12) external profile-specific application property file.
But, as the note in section 2.4. Profile-specific Properties says:
> If you have specified any files in spring.config.location
, profile-specific variants of those files are not considered. Use directories in spring.config.location
if you want to also use profile-specific properties.
Said another way, any file listed in spring.config.location
is by definition a (14) application property file, regardless of how it is named, so (12-13) profile-specific application property files will override them, whether (13) packaged or (12) external.
Summary: Move the test=test in out
property to an application-dev.properties
file in a directory listed in the --spring.config.additional-location
path.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论