Home > database >  Spring property binding with multiple separation
Spring property binding with multiple separation

Time:01-23

I have an application.property like this:

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

I have made a configuration bean like this:

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

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

Is it possible to refer to api.test like apiTest?

Mainly my issue is that I want to use the somevalue starting point for both property. I know if I don't separate with a dot the apiTest and I use it in this way somevalue.api-test I can refer to that with apiTest in my bean, but in my case it's not possible the renaming. So with dot separation can I achieve the same result or I should create two separate config bean, one refering to somevalue.api and the another only to somevalue?

CodePudding user response:

If you can't rename the property then no, you can't reference it using String apiTest. You need an additional class as follows:

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

This should work.

  •  Tags:  
  • Related