@EnableScheduling turns on Spring's scheduled task support. It registers a ScheduledAnnotationBeanPostProcessor, which scans every bean for @Scheduled methods and runs them on a TaskScheduler at the interval or cron time you declare. Without it, @Scheduled annotations are silently ignored and nothing runs.
In Spring Boot you still add the annotation yourself, because Boot configures the scheduler but does not switch scheduling on. The rest of this article covers the three trigger types, how to size the thread pool, and the pitfalls that matter in production.
The annotation imports SchedulingConfiguration, which declares the post-processor bean. When the application context starts, the post-processor inspects each bean method annotated with @Scheduled and registers it as a task. It then looks for a scheduler in this order: a TaskScheduler bean, a ScheduledExecutorService bean, or a local single-thread scheduler it creates itself.
Spring Boot auto-configures a ThreadPoolTaskScheduler (named taskScheduler) when scheduling is enabled, so you usually get a configurable pool without writing any scheduler code.
Put the annotation on any @Configuration class, or directly on your application class.
A scheduled method must take no arguments. Its return value is ignored, so use void. The enclosing class must be a Spring bean.
| Attribute | When the next run starts | Typical use |
|---|---|---|
fixedRate | A fixed interval after the previous run started | Heartbeats, metrics push |
fixedDelay | A fixed interval after the previous run finished | Polling, cleanup that must not overlap |
cron | At matching wall-clock times | Nightly reports, business-hours jobs |
Spring cron expressions have six fields: second, minute, hour, day of month, month, day of week.
You can externalise timings so they change per environment without a rebuild. The *String variants accept property placeholders and ISO-8601 durations.
Always set zone on cron jobs that matter. Otherwise the server's default time zone applies, and daylight saving shifts can move or repeat a run.
By default a plain Spring setup uses a single thread, so one slow job delays every other scheduled job. Spring Boot's auto-configured scheduler also starts with a pool size of 1. Raise it with a property:
Outside Boot, or when you want full control, implement SchedulingConfigurer:
On Spring Boot 3.2 and later, setting spring.threads.virtual.enabled=true on Java 21 or newer makes the auto-configured scheduler use virtual threads. Fixed-rate tasks still run one invocation at a time per method, so a slow method does not overlap with itself.
@Scheduled with @Async without @EnableAsync. Offloading work to another executor needs its own annotation.Yes. Boot auto-configures the TaskScheduler, but only @EnableScheduling (or a manual registration of the post-processor) makes @Scheduled methods run.
fixedRate measures the interval between start times, while fixedDelay measures from the end of one run to the start of the next. Use fixedDelay when runs must never overlap.
No. It must have no parameters, and any return value is discarded. Read inputs from fields or services instead.
Move @EnableScheduling into a separate configuration class guarded by @ConditionalOnProperty or a profile, then leave that property off in tests.
@EnableScheduling registers the post-processor that makes @Scheduled methods run. Pick fixedDelay for non-overlapping polling, fixedRate for steady cadence, and cron with an explicit zone for calendar times. Size the scheduler pool beyond one thread, and add a distributed lock when more than one instance of the application runs.