The @Scheduled annotation marks a method in a Spring bean to be run automatically on a schedule: at a fixed rate, with a fixed delay after the last run, or at times described by a cron expression. It lets you run recurring jobs such as cleanup, report generation or cache refresh without writing timer or thread code.
Scheduling is switched off until you add @EnableScheduling to a configuration class. After that, Spring finds every annotated method on its beans and registers it with a task scheduler.
org.springframework.scheduling.annotation.Scheduled turns an ordinary method into a recurring task. Typical uses are:
It is intended for simple in-process jobs. For jobs that need persistence, retries, a clustered run or a management UI, use a job scheduler such as Quartz, JobRunr or db-scheduler instead.
The method must return void and take no parameters, and its bean must be managed by Spring. A return value is ignored.
| Attribute | Meaning |
|---|---|
fixedRate | Time between the start of one run and the start of the next. |
fixedDelay | Time between the end of one run and the start of the next. |
initialDelay | Wait before the first run, used with either of the above. |
The timeUnit attribute (available since Spring 5.3.10) avoids counting milliseconds. Choose fixedDelay when runs must never overlap, such as work whose duration varies. With fixedRate, a task that runs longer than its interval does not start twice in parallel on the default single-thread scheduler. The next run simply starts late.
There are also fixedRateString, fixedDelayString and initialDelayString, which accept property placeholders and ISO-8601 durations:
Spring cron has six fields: second, minute, hour, day of month, month and day of week. There is no year field.
Always set zone for business schedules. Without it, the server's default time zone is used, which changes between environments and across daylight saving transitions. Setting cron = "-" (or the property value -) disables a task, which is handy for turning it off per environment.
@EnableScheduling. The annotations are silently ignored.spring.task.scheduling.pool.size=4, or turn on virtual threads with spring.threads.virtual.enabled=true.this. The schedule is registered by Spring on the bean, so unit tests should call the method directly instead of waiting for the timer.@Async. If a task must not block the scheduler thread, put the work in an @Async method on another bean.fixedRate measures from the start of each run, so the cadence stays constant. fixedDelay measures from the end of the last run, so a gap is always guaranteed.
Yes. Add @EnableScheduling to a @Configuration class in plain Spring. Spring Boot only adds auto-configured scheduler properties.
No. It must have no arguments. Read what you need from fields, injected beans or a repository.
@Scheduled is fixed at startup. For dynamic schedules, inject a TaskScheduler and call schedule(...), or implement SchedulingConfigurer.
@Scheduled runs a Spring bean method repeatedly at a fixed rate, a fixed delay or a cron time. Enable it with @EnableScheduling, prefer timeUnit and property placeholders over raw milliseconds, set a time zone on cron jobs, and plan for a single scheduler thread and multiple application instances.