title: Spring Boot configuration with pre-set default values?
body: <p>working with configurations in Spring Boot 2.1, I am circling around the question of how to implement a proper configuration management, where I have both parameters from a properties file and hardcoded values als configuration parameters, as well as default values for each parameter. Finally, I would need to have some sort of "integrity check" for each parameter, e.g. checking if an integer is in a specified range.</p>
  
  <p>While this seems easy and simple enough, the realization for this is quite bloated and counterintuitive.</p>
  
  <p>My minimal example looks like this:</p>
  
  <p>application.yml</p>
  
  <pre><code>param1=42
  </code></pre>
  
  <p>Config.java</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:application.yml")
  public class Config {
      private static final int DEFAULT_PARAM1 = 1;
      private static final int PARAM1_MIN = 1;
      private static final int PARAM1_MAX = 5;
  
      @Autowired
      private int param1;
  
      public int getParam1() {
          return param1;
      }
  
      @PostConstruct
      public void init() {
          if(param1 == null || !checkParam1(param1)) param1 = DEFAULT_PARAM1;
      }
  
      public boolean checkParam1(param)
      {
          if(param &gt; PARAM1_MIN || param &lt; PARAM1_MAX)
              return true;
          else return false;
      }
  
      public void set setParam1(int param) {
          if(checkParam1(param))
              param1 = param;
      }
  }
  </code></pre>
  
  <p>Now this seems to be an awful lot of code to simply set ONE parameter. Is there a more elegant, slim, and lean way to do this?</p>