如何从命令行插入其他属性?

时间:2019-07-18 20:30:50

标签: spring-boot kotlin spring-properties

我想让Spring Boot应用程序作为命令行应用程序运行。

我想提供其他属性,这些属性将来自命令行参数,并与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)

}

1 个答案:

答案 0 :(得分:1)

您不需要将属性传递给构建器。 SpringBoot将自动为您合并来自不同来源的属性。有一个处理不同来源的顺序。

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