java / intermediate
Snippet
Scheduling Tasks with Cron Expressions
Spring's @Scheduled annotation with cron expressions provides powerful control over background task execution without external tools.
snippet.java
1
2
3
4
5
6
7
@Componentpublic class ReportScheduler {@Scheduled(cron = "0 0 12 * * MON-FRI")public void sendDailyReport() {// Logic to send report every weekday at 12:00}}
spring
Breakdown
1
cron = "0 0 12 * * MON-FRI"
A cron expression defining execution: seconds, minutes, hours, day-of-month, month, day-of-week.
2
public void sendDailyReport()
The method that Spring will automatically invoke based on the defined schedule.