title: spring @scheduled cron with variable
body: <p>in our system we use a settings class to point to the properties file, depends on which eve it loads different property file. To access specific property, we call 'Settings.getString('property_name_here')'. </p>
  
  <p>In my code, i loaded the @scheduled cron expression to a variable and try to passed to the @scheduled annotation, but it won't work, </p>
  
  <p>here is my code:
  in properties file:</p>
  
  <pre><code>cron.second=0
  cron.min=1
  cron.hour=14
  </code></pre>
  
  <p>in constructor i have:</p>
  
  <pre><code>this.cronExpression = new StringBuilder()
              .append(settings.getString("cron.second"))
              .append(" ")
              .append(settings.getString("cron.min"))
              .append(" ")
              .append(settings.getString("cron.hour"))
              .append(" ")
              .append("*").append(" ").append("*").append(" ").append("*")
              .toString();
  </code></pre>
  
  <p>which create a String of "0 1 14 * * *", it's a valid con expression</p>
  
  <p>in the scheduled task i have:</p>
  
  <pre><code>@Scheduled(cron = "${this.cronExpression}")
  public void scheduleTask() throws Exception {
           ....
   }
  </code></pre>
  
  <p>when I ran the code it complaint:
  Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'scheduleTask': Cron expression must consist of 6 fields (found 1 in "${this.cronExpression}")</p>
  
  <p>then I changed this.cronExpression to a list of String:</p>
  
  <pre><code>    this.cronExpression = Lists.newArrayList();
      this.cronExpression.add(settings.getString("cron.second"));
      this.cronExpression.add(settings.getString("cron.min"));
      this.cronExpression.add(settings.getString("cron.hour"));
      this.cronExpression.add("*");
      this.cronExpression.add("*");
      this.cronExpression.add("*");
  </code></pre>
  
  <p>but still got the same error, so what exactly is the cron expression supposed to be? </p>