📖 Scenario: You are building a Spring Boot application that needs to run tasks automatically at specific times. You will learn how to use cron expressions to schedule these tasks.
🎯 Goal: Create a Spring Boot component with a scheduled method that runs every minute using a cron expression.
📋 What You'll Learn
Create a Spring Boot component class named ScheduledTasks
Add a method named reportCurrentTime that prints the string "Current time reported"
Use the @Scheduled annotation with a cron expression to run the method every minute
Enable scheduling in the Spring Boot application
💡 Why This Matters
🌍 Real World
Scheduling tasks is common in real applications for things like sending emails, cleaning databases, or generating reports automatically.
💼 Career
Understanding cron expressions and scheduling in Spring Boot is essential for backend developers working on automation and timed processes.
Progress0 / 4 steps
1
Create the ScheduledTasks component
Create a class named ScheduledTasks annotated with @Component.
Spring Boot
Hint
Use @Component to make the class a Spring bean.
2
Add the reportCurrentTime method
Inside the ScheduledTasks class, add a public method named reportCurrentTime that returns void and prints the string "Current time reported".
Spring Boot
Hint
Define a method that prints the message to the console.
3
Schedule the method with a cron expression
Add the @Scheduled annotation to the reportCurrentTime method with the cron expression "0 * * * * *" to run it every minute.
Spring Boot
Hint
Use @Scheduled(cron = "0 * * * * *") to run the method every minute.
4
Enable scheduling in the application
In your main Spring Boot application class, add the @EnableScheduling annotation above the class declaration.
Spring Boot
Hint
Add @EnableScheduling to activate scheduled tasks in Spring Boot.
Practice
(1/5)
1. What does a cron expression in Spring Boot primarily specify?
easy
A. The output format of the task
B. When a scheduled task should run
C. The name of the scheduled task
D. The priority of the task
Solution
Step 1: Understand the purpose of cron expressions
Cron expressions define timing details for scheduling tasks.
Step 2: Identify what cron expressions control in Spring Boot
They tell Spring Boot exactly when to run a method automatically.
Final Answer:
When a scheduled task should run -> Option B
Quick Check:
Cron expression = schedule time [OK]
Hint: Cron expressions set task run times, not names or outputs [OK]
Common Mistakes:
Thinking cron sets task names
Confusing cron with output formatting
Assuming cron controls task priority
2. Which of the following is the correct syntax to schedule a method to run every minute using Spring Boot's @Scheduled annotation with a cron expression?
Hour field is 25, which is outside 0-23 range, causing error.
Final Answer:
Hour value 25 is invalid -> Option C
Quick Check:
Hour must be 0-23, 25 is invalid [OK]
Hint: Hours max 23 in cron, check all field ranges [OK]
Common Mistakes:
Using hour > 23
Forgetting seconds field
Misusing '?' in day or weekday
5. You want to schedule a Spring Boot task to run every Monday at 8:30 AM, but only during the months of June, July, and August. Which cron expression correctly achieves this?
hard
A. "0 30 8 ? 6,7,8 MON"
B. "0 30 8 * 6-8 *"
C. "0 30 8 ? 5-8 MON"
D. "0 30 8 ? 6-8 1"
Solution
Step 1: Understand cron fields needed
Seconds=0, Minutes=30, Hours=8, Day=? (no specific day), Month=6,7,8 (June-Aug), Weekday=MON (Monday)
Step 2: Check options for correct syntax
"0 30 8 ? 6,7,8 MON" uses '?' for day, lists months as 6,7,8, and weekday as MON, which is correct.
Final Answer:
"0 30 8 ? 6,7,8 MON" -> Option A
Quick Check:
Use '?' for day, list months, specify MON weekday [OK]
Hint: Use '?' for day when specifying weekday, list months with commas [OK]
Common Mistakes:
Using '*' for day and weekday together
Using range '-' instead of commas for months
Using numeric weekday instead of MON
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.