Spring 引导 2:使用外部 属性-文件

Spring Boot 2: Using external Property-Files

我有一个可执行的 jar,我想通过 jar 外部的属性进行配置。当把它放在靠近 jar 的 config 文件夹中时,工作正常的是 application.properties。但是我有第二个 属性-file 似乎没有被拾起,我想有最好的做法。

文件夹 config 如下所示:

在配置文件夹中,您会发现:

两个 属性 文件也在 src/main/resources 文件夹中。

我的 StartClass 看起来像:

@SpringBootApplication
@PropertySource("migration-shrink.properties")
public class MigrationShrinkApplication implements CommandLineRunner {}

我的 bat 文件如下所示:

java -jar migration-shrink-0.0.1-SNAPSHOT.jar -Dspring.config.location=./config/migration-shrink.properties

我想将 Spring-配置与应用程序配置分开,这就是为什么我有两个不同的 属性-文件。

谢谢!

不需要 @PropertySource 注释。

从 Spring Boot 2.0 开始,您可以声明其他位置:

-Dspring.config.additional-location=./config/migration-shrink.properties

请记住,这些附加位置会先于其他位置被搜索,因此其他位置的值可能会被覆盖。

the Spring Boot reference documentation