To implement a cron job in Spring Boot, add @EnableScheduling to a configuration class and annotate a no-argument method of a Spring bean with @Scheduled(cron = "..."). Spring uses a six-field cron expression (second, minute, hour, day of month, month, day of week) and runs the method on its task scheduler, with no external cron daemon needed.
The default scheduler is single-threaded and every application instance runs its own jobs, so a few settings matter once you go beyond a demo: the time zone, the thread pool size, and what happens when you run several instances.
Spring parses cron expressions with CronExpression and expects six fields, separated by spaces. This differs from the five-field Unix crontab, which has no seconds field.
| Position | Field | Allowed values |
|---|---|---|
| 1 | Second | 0-59 |
| 2 | Minute | 0-59 |
| 3 | Hour | 0-23 |
| 4 | Day of month | 1-31 |
| 5 | Month | 1-12 or JAN-DEC |
| 6 | Day of week | 0-7 (0 and 7 are Sunday) or MON-SUN |
Common special characters are * (every value), , (a list), - (a range), / (a step) and ? (no specific value). Spring 5.3 and later also accept L (last day), W (nearest weekday) and # (nth weekday of the month), plus the macros @hourly, @daily, @weekly, @monthly and @yearly.
| Expression | Meaning |
|---|---|
0 0 2 * * * | Every day at 02:00:00 |
0 */5 * * * * | Every 5 minutes |
0 30 9 * * MON-FRI | 09:30 on weekdays |
0 0 0 1 * * | Midnight on the first day of every month |
@daily | Every day at midnight |
You can check an expression before shipping it:
CronExpression.parse throws IllegalArgumentException for an invalid expression, which is useful for validating user-supplied schedules.
Spring Boot needs no extra dependency, because scheduling lives in spring-context. Enable it once, then add scheduled methods to any bean.
The annotated method must return void (any return value is ignored) and take no parameters. Always set zone for jobs that must fire at a business-relevant local time; without it the JVM default time zone is used, which differs between a laptop and a container running in UTC. Daylight saving changes can skip or repeat a local time, so avoid scheduling important jobs at 02:00 to 03:00 in zones that change clocks.
@Scheduled supports three trigger styles. Pick the one that matches the requirement.
| Attribute | When it runs | Typical use |
|---|---|---|
cron | At calendar times | Nightly reports, month-end jobs |
fixedRate | Every N ms measured from the start of the previous run | Heartbeats, metrics |
fixedDelay | N ms after the previous run finished | Polling, queue draining |
The ...String variants accept ISO-8601 durations such as PT30S or property placeholders, so the value can live in configuration.
Hard-coded expressions are hard to change per environment. Read them from properties instead:
To switch a job off in an environment, set the property to the special value -. Spring treats it as a disabled trigger (the constant Scheduled.CRON_DISABLED), and the method is never called.
By default Spring Boot auto-configures a ThreadPoolTaskScheduler with a pool size of 1. One slow job therefore delays every other scheduled job. Raise the pool size with a property:
On Java 21 or later with Spring Boot 3.2 or newer, you can run scheduled work on virtual threads with spring.threads.virtual.enabled=true. Scheduling then uses a SimpleAsyncTaskScheduler, which suits blocking I/O jobs.
A single @Scheduled method is not started again while its previous run is still going on the same scheduler. If you add @Async to the method, runs can overlap, so use it only for jobs that are safe to run concurrently.
@Scheduled has no cluster awareness. If you run three replicas, the job fires three times. Options:
CronJob.spring-boot-starter-quartz) when you need persistent, clustered, programmatically managed schedules.Keep jobs idempotent anyway, because a retry or a restart can always run one twice.
Do not wait for the clock in tests. Keep the method body thin, move the logic into a service, and test the service directly. To check the schedule itself, validate the expression:
For an integration test that needs the scheduler to fire, Awaitility lets you wait for a side effect with a timeout instead of sleeping.
@Scheduled method never called?The usual causes are a missing @EnableScheduling, a class that is not a Spring bean, a method that takes parameters, or a disabled expression (-). Check that the class is picked up by component scanning.
No. Spring requires six fields with seconds first. A five-field expression fails with an IllegalArgumentException at startup.
Use a distributed lock such as ShedLock, or move the job to a single instance or a Kubernetes CronJob.
Not with @Scheduled. Implement SchedulingConfigurer and register a trigger that reads the expression from a database or property source, or use Quartz.
Add @EnableScheduling, annotate a void, no-argument bean method with @Scheduled(cron = "..."), and remember that Spring cron has six fields starting with seconds. Set zone explicitly, move expressions into properties, increase the scheduler pool size beyond the default of one, and use a lock when you run more than one instance.