title: Spring Boot initialize new instance for nested configuration binding when there are no corresponding nested key/value pairs in application.properties
body: <p>I have a configuration class like below. All of fields in the inner class <code>OptionalServiceConfigs</code> has a default value as annotated using <code>@Value</code> as shown in below.</p>
  
  <p>Sometimes in my <code>application.properties</code> file, it <strong>does not</strong> have a single <code>service</code> prefixed property. In that case, we want to have loaded an <code>OptionalServiceConfigs</code> instance with its default field values.</p>
  
  <pre><code>@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
  }
  </code></pre>
  
  <p>But unfortunately, the <code>service</code> field is <code>null</code> when it is accessed using its getter method. Because spring boot <strong>does not</strong> initialize an instance of it when there is no property keys found with prefixed <code>myconf.service.*</code> in my <code>application.properties</code> file.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>How can I make <code>service</code> field to initialize to a new instance along with its specified default field values when there are no corresponding prefixed keys in properties file?</p>
  
  <p>I can't imagine a value to put in annotation <code>@Value("${service:?????}")</code> for <code>service</code> field. 
  Nothing works, tried, <code>@Value("${service:}")</code> or <code>@Value("${service:new")</code></p>