【问题标题】:Spring Boot 2: Using external Property-FilesSpring Boot 2:使用外部属性文件
【发布时间】:2018-08-01 07:21:10
【问题描述】:

我有一个可执行的 jar,我想通过 jar 外部的属性来配置它。工作正常的是application.properties,将其放入靠近罐子的config 文件夹中。但我有第二个属性文件似乎没有被拾取,我希望有最好的做法。

文件夹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-Configuration 与 Application-Configuration 分开,这就是为什么我有两个不同的属性文件。

谢谢!

【问题讨论】:

    标签: spring-boot properties external executable-jar


    【解决方案1】:

    @PropertySource 注释不是必需的。

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

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

    请记住,这些额外的位置会先于其他位置进行搜索,因此可以覆盖其他位置中的值。

    the Spring Boot reference documentation

    【讨论】:

    • 嗨,Brian,谢谢你的回答,-Dspring.config.additional-location 非常有帮助,而且效果很好,尤其是我只需要将属性放入对用户配置有意义的外部配置文件中,其他属性默认情况下在src/main/resources 中设置。 @PropertySourcemigration-shrink.properties 我仍然需要。删除它的效果是找不到属性。因此,如果您使用 @PropertySource 删除提示,我会给您支票。
    • 等等 - 这些附加属性是否映射到 Spring Boot 配置属性类?还是@Value注解使用的那些?
    • 很少与@Value一起使用
    猜你喜欢
    • 2020-07-30
    • 2018-02-21
    • 2015-06-26
    • 2016-09-15
    • 2015-05-14
    • 2018-02-25
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多