I have a list of values in one property in application.properties
file:
my-property=abc,def,ghi
And I load it in my @ConfigurationProperties
class:
@Configuration
@ConfigurationProperties
public class MyProperties {
private List<String> myProperty;
}
But I don't know how to change delimiter (from comma to semicolon or other separator):
my-property=abc;def;ghi
I know I can write workaround, but I don't want to use them because:
@ConfigurationProperties
advantages).@Value("#{'${my-property}'.split(';')}")
private List<String> myProperty;
my-property[0]=abc
my-property[1]=def
my-property[2]=ghi
split
, but I have to write getter for all properties manually:private String myProperties;
public List<String> getMyProperty() {
return Arrays.asList(myProperties.split(";"));
}
The answer is annotation @Delimiter
.
You can set any string as delimiter (default value is ,
). For example:
@Configuration
@ConfigurationProperties
public class MyProperties {
@Delimiter(";")
private List<String> myProperty;
}
and application.properties
file:
my-property=abc;def;ghi
Longer string as delimiters also works. For example annotation @Delimiter("---")
with property:
my-property=abc---def---ghi
If you interested, I found this annotation by debugging Spring source code. The key is class DelimitedStringToArrayConverter
(check source code to find the annotation). You can see default value ,
when delimiter is null.
How it works:
public void setMyProperty(List<String> myProperty) {
this.myProperty = myProperty;
}
CollectionBinder
IndexedElementsBinder
BindConverter
ApplicationConversionService
DelimitedStringToArrayConverter