在 application.yml 中为单个 属性 添加多个持续时间项

Add multiple duration terms in application.yml for single property

在 Spring 引导中,可以在 application.yml 中设置配置变量,如下所示:

myprogram:
   my-property: 5d # respectively 5 days

并且在代码中可以像这样检索它:

@ConfigurationProperties("myprogram")
public class MyProgramProperties {

    @DurationUnit(ChronoUnit.SECONDS)
    private Duration myProperty = Duration.ofSeconds(30); // default = 30 sec, if config var is not set

    public Duration getMyProperty() {
        return this.myProperty ;
    }

    public void setMyProperty(Duration myProperty ) {
        this.myProperty = myProperty ;
    }

}

但是是否有可能有以下内容,如果有的话如何实现:

myprogram:
   my-property: 5d 3h 5m # respectively 5 days, 3 hours, 5 minutes

提前致谢。

我不这么认为,您需要将其视为 String 并使用单独的 get 方法来提取您想要的内容。