首页 > 解决方案 > 如何从命令行插入其他属性?

问题描述

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

我想提供来自命令行参数并与属性合并的其他属性application.yaml

当我使用Propertiesthen时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)

}

标签: spring-bootkotlinspring-properties

解决方案


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

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


推荐阅读