Spring 属性 多重分离绑定

Spring property binding with multiple separation

我有一个 application.property 这样的:

somevalue.api.test=something
somevalue.anotherproperty=stuff

我做了一个这样的配置bean:

@Configuration
@ConfigurationProperties("somevalue")
public class SomeProperties {

    @NotNull
    private String apiTest;
    @NotNull
    private String anotherproperty;
}

是否可以像apiTest一样引用api.test

主要是我的问题是我想对两个 属性 使用 somevalue 起点。我知道如果我不用点分隔 apiTest 并且我以这种方式使用它 somevalue.api-test 我可以在我的 bean 中用 apiTest 引用它,但在我的情况下不可能重命名。因此,通过点分隔,我能否获得相同的结果,或者我应该创建两个单独的配置 bean,一个引用 somevalue.api,另一个仅引用 somevalue?

如果您不能重命名 属性,那么不,您不能使用 String apiTest 引用它。您需要一个额外的 class 如下:

@Configuration
@ConfigurationProperties("somevalue")
public class GcssProperties {
    @NotNull
    private GcssApiProperties api;
    
    @NotNull
    private String anotherproperty;
}
public class GcssApiProperties {
    @NotNull
    private String test;
}

这应该有效。