如何从命令行插入附加属性?

How to insert additional properties from command line?

我想将 Spring 启动应用程序 运行 作为命令行应用程序。

我想提供来自命令行参数的额外属性,并与 application.yaml

中的属性合并

当我使用 Properties 时,application.yaml 被省略。我如何合并来自两个来源的属性?

@SpringBootApplication
class MyMain

fun main(args: Array<String>) {

    val properties = Properties().apply {
        setProperty("foo", // value from command line args)
    }

    SpringApplicationBuilder(MyMain::class.java)
        .web(WebApplicationType.NONE)
        .properties(properties)
        .initializers(BeansInitializer())
        .run(*args)

}

您不需要将属性传递给生成器。 SpringBoot 会自动为你合并来自不同来源的属性。处理不同的来源是有顺序的。

看这里:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html