Search code examples
springcronschedule

How to config cron value of @Scheduled in application.properties


I am using spring-schedule like this.

@Component
@EnableScheduling
public class ScheduledTasks {

    @Autowired
    private ISomeJob someJob;

    /**
     * do a Job every 5 minutes.
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void foo(){
        someJob.doSomething();
    }
}

It worked. But there is a problem.
I have two profiles named debug and release.
I want do this job every 5 minutes in debug but per hour in release.
So is there any way to config the value of cron in application.properties.


Solution

  • Just add an expression @Scheduled(cron = "${some.profile.cron}") to swap the cron depending on selected profile.