I have a configuration class like below. All of fields in the inner class OptionalServiceConfigs
has a default value as annotated using @Value
as shown in below.
Sometimes in my application.properties
file, it does not have a single service
prefixed property. In that case, we want to have loaded an OptionalServiceConfigs
instance with its default field values.
@Configuration
@ConfigurationProperties(prefix = "myconf")
public class MyConfigs {
// ... rest of my configs
@Value("${service:?????}") // what to put here, or can I?
private OptionalServiceConfigs service; // this is null
// In this class all fields have a default value.
public static class OptionalServiceConfigs {
@Value("${mode:local}")
private String mode;
@Value("${timeout:30000}")
private long timeout;
// ... rest of getter and setters
}
// ... rest of getter and setters
}
But unfortunately, the service
field is null
when it is accessed using its getter method. Because spring boot does not initialize an instance of it when there is no property keys found with prefixed myconf.service.*
in my application.properties
file.
Question:
How can I make service
field to initialize to a new instance along with its specified default field values when there are no corresponding prefixed keys in properties file?
I can't imagine a value to put in annotation @Value("${service:?????}")
for service
field.
Nothing works, tried, @Value("${service:}")
or @Value("${service:new")