【问题标题】:How do I change the values of application.yaml at runtime?如何在运行时更改 application.yaml 的值?
【发布时间】:2019-08-02 15:31:30
【问题描述】:

如何在运行时更改 application.yaml 的值 例如,我有一个服务器地址属性,我想在运行时更改

服务器: 地址:192.168.1.100

【问题讨论】:

标签: spring spring-boot


【解决方案1】:

我假设这是一个 spring 应用程序,如果是这样,您可以使用 jvm 参数 (-D) 覆盖 application.yaml 文件中的值。 例如 应用程序.yaml

server:
  address: 192.168.0.1

命令行

java -jar -Dserver.address=10.10.0.1

【讨论】:

  • 那不是真正的运行时 :D 那是启动时的属性!
【解决方案2】:

如果您需要覆盖许多属性,您也可以使用单独的文件

@SpringBootApplication
@PropertySources({
        @PropertySource(name = "default", value = "classpath:default.yaml"),
        @PropertySource(name = "external", value = "file:${custom.properties:}", ignoreResourceNotFound = true)
})
public class BootApplication { ... }

然后像这样启动

java -jar -Dcustom.properties=/path/to/custom.yaml

custom.yaml 包含在哪里

server: 
   address: 10.10.10.100
   port: 8888
   etc: blabla
   ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多