In the Spring Framework, @Scheduled marks a method to be invoked repeatedly by a task scheduler, either at a fixed interval, after a fixed delay, or at times given by a cron expression. It is part of Spring's task execution and scheduling support, and it works in any Spring application, with or without Spring Boot.
To use it you enable scheduling support, annotate a method on a Spring bean, and make sure a scheduler is available. Spring supplies a default single-threaded one if you do not.
Without @Scheduled, recurring work needs java.util.Timer or a ScheduledExecutorService that you create, start and stop. @Scheduled moves that into configuration. The bean lifecycle manages the scheduler, tasks can use injected beans and transactions, and the schedule lives next to the code it triggers.
Common use cases:
Use Java configuration:
The XML equivalent is <task:annotation-driven/> from the task namespace, which also lets you set a scheduler reference. In a Spring Boot application, @EnableScheduling on the main class is enough, since the auto-configured scheduler is picked up automatically.
reportCurrentTime() runs every five seconds. Prefer a logger over System.out, since it gives you timestamps and thread names for free, which helps when debugging schedules.
fixedRate triggers at constant intervals measured from the start of each run.
fixedDelay waits for the given time after each run completes. Use it when runs must not crowd each other.
cron uses six fields: second, minute, hour, day of month, month, day of week.
| Expression | Meaning |
|---|---|
0 0 9 * * MON | Every Monday at 09:00 |
0 0 9 5 * * | The 5th day of every month at 09:00 |
0 */15 * * * * | Every 15 minutes |
0 0 22 * * MON-FRI | 22:00 on weekdays |
Specify zone so the schedule does not depend on the server's default time zone. Other attributes include initialDelay, the *String variants for placeholders, and timeUnit (since Spring 5.3.10).
Annotation values are fixed when the context starts. When you need a schedule stored in a database or changed at runtime, implement SchedulingConfigurer, or inject a TaskScheduler and call it directly.
scheduleWithFixedDelay returns a ScheduledFuture, which you keep if you want to cancel the job later.
ThreadPoolTaskScheduler bean.void.The Spring Framework, in spring-context. Spring Boot adds convenient scheduler properties but does not own the annotation.
Use TaskScheduler.schedule(runnable, Instant). @Scheduled always creates recurring tasks.
The common causes are a missing @EnableScheduling, a bean that is not managed by Spring, a cron with five fields instead of six, or a time zone different from what you expected.
@Scheduled is Spring's annotation for recurring tasks, driven by fixedRate, fixedDelay or cron. Enable it with @EnableScheduling or <task:annotation-driven/>, set a time zone for cron expressions, size the scheduler for your workload, and use TaskScheduler or a dedicated job library for dynamic or clustered scheduling.