Spring Boot @Scheduled Cron Generator
Spring cron uses 6 fields: second minute hour day month weekday. The day-of-week and day-of-month conflict is handled with ? - if you set a specific weekday, day must be * or ?.
Quick presets
0 * * * * ?
Runs every minute
Use in Spring Boot @Scheduled
@Scheduled annotation:
@Component
public class MyScheduledTask {
@Scheduled(cron = "0 * * * * ?")
public void runTask() {
// task logic here
}
}
Using a property (recommended for production):
@Scheduled(cron = "${my.task.cron}")
public void runTask() { ... }
In application.properties:
my.task.cron=0 * * * * ?
@Scheduled Cron vs Fixed Rate
@Scheduled accepts three different attributes - they are not interchangeable. Picking the wrong one is the most common Spring scheduling mistake.
| Attribute | Meaning | Example |
|---|---|---|
cron | Calendar-based schedule, like crontab | cron = "0 0 9 * * MON-FRI" |
fixedRate | Run every N ms, measured from start to start | fixedRate = 5000 |
fixedDelay | Run every N ms, measured from end to next start | fixedDelay = 5000 |
Use cron whenever you need a specific time of day or day of week - the other two only express simple repeating intervals from app startup, not wall-clock time.
Spring vs Linux Cron
| Feature | Linux | Spring Boot |
|---|---|---|
| Fields | 5 | 6 |
| Field order | min hr dom month dow | sec min hr dom month dow |
| Seconds | No | Yes (1st field) |
? operator | No | Yes |
L operator | No | Yes |
| Timezone | Server TZ | App TZ (configurable) |
@Scheduled Setup Tips
Enable scheduling first
@Scheduled does nothing on its own. Add @EnableScheduling to your main class or a configuration class, or the annotation is silently ignored:
@SpringBootApplication
@EnableScheduling
public class MyApp { ... }
Set timezone per task
Available from Spring 5.3+:
@Scheduled(cron = "0 0 9 * * MON-FRI",
zone = "Europe/Madrid")
public void morningTask() { ... }
Use properties, not hardcoded values
Hardcoding cron expressions in annotations makes them impossible to change without redeployment.
Use ${property.name} so you can change the schedule via application.properties or environment variables.
See the Spring @Scheduled Javadoc.
@Scheduled methods must return void
A scheduled method's return value is always ignored, since nothing is waiting to receive it. Methods must also take no arguments - Spring has nothing to pass in.
Spring Batch scheduled jobs
For Spring Batch, trigger a JobLauncher from a @Scheduled method.
Same cron syntax applies - 6 fields, seconds first.
Spring Boot Cron Expression Examples
| Expression | Description | |
|---|---|---|
0 * * * * ? | Every minute at second 0 | |
0 */5 * * * ? | Every 5 minutes | |
0 0 * * * ? | Every hour on the hour | |
0 0 0 * * ? | Every day at midnight | |
0 0 9 * * MON-FRI | Weekdays at 9:00 AM | |
0 0 0 1 * ? | First of every month at midnight | |
0 0 0 L * ? | Last day of every month | |
0 30 8 * * MON | Every Monday at 8:30 AM | |
0 0 6,18 * * ? | Twice daily at 6 AM and 6 PM | |
0 0 0 ? * 2#1 | First Monday of every month |