ScheduledTaskRegistrar is the Spring class that holds and registers scheduled tasks. Spring creates one internally for every @Scheduled method, and you get direct access to it by implementing SchedulingConfigurer. Use it when @Scheduled is too static: when intervals or cron expressions must come from a database or configuration at runtime, when you need a custom TaskScheduler, or when you register tasks in code.
The annotation processor and the registrar share the same scheduler, so you can mix both styles in one application.
When @EnableScheduling is active, ScheduledAnnotationBeanPostProcessor scans beans for @Scheduled methods and registers each one. Before it starts the tasks, it calls every SchedulingConfigurer bean with its ScheduledTaskRegistrar, which is your chance to add tasks or replace the scheduler.
| Concern | @Scheduled | ScheduledTaskRegistrar |
|---|---|---|
| Definition | Annotation on a method | Java code in configureTasks |
| Interval or cron | Fixed at compile time (or a property placeholder) | Computed at runtime |
| Custom scheduler | Indirect, through a bean | setScheduler(...) or setTaskScheduler(...) |
| Tasks created in loops | No | Yes |
Implement SchedulingConfigurer on a @Configuration class that also has @EnableScheduling. The registrar offers addFixedRateTask, addFixedDelayTask, addCronTask and addTriggerTask.
This registers three tasks on a four-thread scheduler. The cron expression has six fields in Spring (second, minute, hour, day of month, month, day of week), so 0 0 2 * * * means every day at 02:00. Without your own scheduler, Spring falls back to a single-threaded local executor, which is why one long task can delay all others.
The most common reason to use the registrar is a schedule that can change without a restart. A Trigger is asked for the next execution time after every run, so a task can read a new value each time.
Each time the task finishes, Spring calls the lambda, which builds a CronTrigger from the current expression. After schedule.update("0 0 * * * *"), the next computed run follows the new expression. This lambda form uses the Instant-based nextExecution method of Spring Framework 6 and later; on Spring 5 you implement nextExecutionTime and return a Date. Validate new expressions with CronExpression.isValidExpression(...) before accepting them.
CronTrigger accepts a ZoneId, so you can schedule in a business time zone instead of the server's: new CronTrigger("0 0 9 * * MON-FRI", ZoneId.of("Europe/Berlin")). Remember daylight saving changes when you pick hours near the switch.
TaskScheduler and call schedule(...), keeping the returned ScheduledFuture so you can cancel(false) later.@Scheduled declares a fixed schedule on a method. ScheduledTaskRegistrar lets you register tasks in code and compute their timing at runtime. Both end up on the same scheduler.
Yes. @EnableScheduling activates the infrastructure that discovers SchedulingConfigurer beans and starts the tasks.
Provide a ThreadPoolTaskScheduler with a pool size above one and pass it to registrar.setTaskScheduler(...). Spring Boot users can also set spring.task.scheduling.pool.size.
Not through the registrar in a supported way. Use a TaskScheduler bean and manage the ScheduledFuture objects yourself.
ScheduledTaskRegistrar is the programmatic side of Spring scheduling. Implement SchedulingConfigurer to register fixed-rate, fixed-delay, cron and trigger tasks, to supply your own TaskScheduler and to build schedules that change at runtime. For schedules known at compile time, @Scheduled remains simpler.