Spring Boot @Scheduled cron

Viewed 14191

Is there a way to call a getter (or even a variable) from a propertyClass in Spring's @Scheduled cron configuration? The following doesn't compile:

@Scheduled(cron = propertyClass.getCronProperty()) or @Scheduled(cron = variable)

I would like to avoid grabbing the property directly:

@Scheduled(cron = "${cron.scheduling}")
3 Answers

If you don't want to retrieve the cron expression from a property file you can do it programatically as it follows:

// Constructor
public YourClass(){
   Properties props = System.getProperties();
   props.put("cron.scheduling", "0 30 9 * * ?");
}

That allows you to use your code whitout any changes:

@Scheduled(cron = "${cron.scheduling}")
Related