In SpringBoot I want to create a simple constant class to be populated from application.yaml
. I have found a solution leveraging Lombok.
@RequiredArgsConstructor
@ConfigurationProperties("app")
public final class PropertiesConstants {
public final String MY_FIRST_PROPERTY;
}
This is pretty neat and straight forward. I want to extend the solution with default value if the property in application.yaml
is missing.
The simplest solution I found is following:
@ConfigurationProperties("app")
public final class PropertiesConstantsDefaults {
public final String MY_FIRST_PROPERTY;
public PropertiesConstantsDefaults(
@DefaultValue("default")
String myFirstProperty
) {
MY_FIRST_PROPERTY = myFirstProperty;
}
}
This works but it is by far not as short and straight forward as the first one. I wonder if there is a better option.